From d0cb51039bda085b02a233171d55329b17e8d4e4 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 18 Jul 2024 13:04:22 +0100 Subject: [PATCH] Bug 37389: Add a builder to dynamically create relations when required This private method is intended to be called from the API query builder to add relation accessors for later use in the search query --- Koha/ILL/Requests.pm | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Koha/ILL/Requests.pm b/Koha/ILL/Requests.pm index 468bd34d825..8c5c27765f4 100644 --- a/Koha/ILL/Requests.pm +++ b/Koha/ILL/Requests.pm @@ -108,6 +108,47 @@ sub search_incomplete { =head2 Internal methods + +=head3 _build_extended_attributes_relations + +Method to dynamically add has_many relations for each ILLRequestAttribute type stored in the ILLRequestAttributes table. + +Used in the API to allow for advanced joins. + +Returns a list of relation accessor names. + +=cut + +sub _build_extended_attributes_relations { + my ($self) = @_; + + my @distinct_types = $self->_resultset->search( + {}, # No specific conditions + { + select => ['type'], + distinct => 1, + } + )->get_column('type')->all; + + for my $type (@distinct_types) { + $self->_resultset->has_many( + "extended_attributes_$type", + "Koha::Schema::Result::Illrequestattribute", + sub { + my $args = shift; + + return { + "$args->{foreign_alias}.illrequest_id" => { -ident => "$args->{self_alias}.illrequest_id" }, + "$args->{foreign_alias}.type" => { '=', $type }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, + ); + } + + return map { 'extended_attributes_'.$_ } @distinct_types; +} + =head3 _type =cut -- 2.45.2