diff --git a/Koha/ActionLog.pm b/Koha/ActionLog.pm index 6a175bb6dd..410a46e09e 100644 --- a/Koha/ActionLog.pm +++ b/Koha/ActionLog.pm @@ -21,6 +21,11 @@ use Modern::Perl; use C4::Context; +# Avoid `use Koha::Patrons` here: Koha::ActionLog is loaded transitively by +# C4::Log very early in the dependency chain, before Koha::Patron has been +# fully defined. The accessors below reach for Koha::Patron at call time, +# by which point it is reliably available. + use base qw(Koha::Object); =head1 NAME @@ -31,8 +36,40 @@ Koha::ActionLog - Koha ActionLog Object class =head2 Class methods +=head3 librarian + + my $librarian = $log->librarian; + +Returns the related I object for the librarian who performed +the action, or I if the C column was not set (e.g. cron jobs). + =cut +sub librarian { + my ($self) = @_; + my $rs = $self->_result->librarian; + return unless $rs; + return Koha::Patron->_new_from_dbic($rs); +} + +=head3 patron + + my $patron = $log->patron; + +Returns the related I object joined on the C column. +Only meaningful for rows where C is a borrowernumber (MEMBERS, +CIRCULATION, FINES and APIKEYS modules); for other modules the join may +match an unrelated patron and the caller must filter by C. + +=cut + +sub patron { + my ($self) = @_; + my $rs = $self->_result->patron; + return unless $rs; + return Koha::Patron->_new_from_dbic($rs); +} + =head2 Internal methods =head3 _type diff --git a/Koha/Schema/Result/ActionLog.pm b/Koha/Schema/Result/ActionLog.pm index c97ffdcf21..2fa580cb2b 100644 --- a/Koha/Schema/Result/ActionLog.pm +++ b/Koha/Schema/Result/ActionLog.pm @@ -154,6 +154,41 @@ __PACKAGE__->set_primary_key("action_id"); # Created by DBIx::Class::Schema::Loader v0.07051 @ 2024-05-02 14:28:43 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:8ZJaG4iJAbay93LAg5xPsQ +# Both relations below are pseudo-relations: action_logs.user and +# action_logs.object have no SQL foreign-key constraint on borrowers, +# so we set is_foreign_key_constraint => 0 to keep TestBuilder from +# trying to materialise a Borrower row when building action_logs +# fixtures. +__PACKAGE__->belongs_to( + "librarian", + "Koha::Schema::Result::Borrower", + { borrowernumber => "user" }, + { + is_foreign_key_constraint => 0, + join_type => "LEFT", + }, +); + +# `object` is polymorphic across modules (borrowernumber, biblionumber, +# subscriptionid, ...). The relation is only meaningful for MEMBERS, +# CIRCULATION, FINES and APIKEYS rows; other modules may produce a +# coincidentally-matching borrowernumber row that callers must ignore. +__PACKAGE__->belongs_to( + "patron", + "Koha::Schema::Result::Borrower", + { borrowernumber => "object" }, + { + is_foreign_key_constraint => 0, + join_type => "LEFT", + }, +); + +sub koha_object_class { + 'Koha::ActionLog'; +} + +sub koha_objects_class { + 'Koha::ActionLogs'; +} -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1;