From be855a146839cac4aa2f8bbb48b1e0c705489a72 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 18 Jul 2024 14:11:06 +0100 Subject: [PATCH] Bug 37389: Add AUTOLOAD to dynamically add relation accessors This patch adds an AUTOLOAD to Koha::ILL::Request to allows fetching extended_attributes_$type attributes via dynamically added relations added into the underlying DBIC resultset class. --- Koha/ILL/Request.pm | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Koha/ILL/Request.pm b/Koha/ILL/Request.pm index e7347e5ee32..24c3a2222f9 100644 --- a/Koha/ILL/Request.pm +++ b/Koha/ILL/Request.pm @@ -2220,6 +2220,35 @@ sub get_staff_table_actions { return $ill_table_actions; } +=head3 AUTOLOAD + +=cut + +our $AUTOLOAD; + +sub AUTOLOAD { + my ($self) = @_; + + my $name = $AUTOLOAD; + $name =~ s/.*:://; # Remove package name + + if ( $name =~ /^extended_attributes_(\w+)$/ ) { + my $type = $1; + + # Define the method dynamically + no strict 'refs'; + *{$AUTOLOAD} = sub { + my ($self) = @_; + my $relation = 'extended_attributes_' . $type; + my $rs = $self->_result->$relation; + return Koha::ILL::Request::Attributes->_new_from_dbic($rs)->search; + }; + + # Call the newly defined method + return $self->$name(); + } +} + =head3 _type =cut -- 2.45.2