From 031d6e37f908efe8d7bcf7928f8a2578ba80be1f Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 18 Jul 2024 17:33:37 +0100 Subject: [PATCH] Bug 37389: Working accessors --- Koha/ILL/Requests.pm | 59 +++++++++++++++------------------------ Koha/REST/Plugin/Query.pm | 7 +++-- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/Koha/ILL/Requests.pm b/Koha/ILL/Requests.pm index cdf2a9de4a3..a8435d1b6a1 100644 --- a/Koha/ILL/Requests.pm +++ b/Koha/ILL/Requests.pm @@ -119,43 +119,28 @@ Returns a list of relation accessor names. =cut -# sub _build_extended_attributes_relations { -# my ($self) = @_; - -# my @distinct_types = Koha::ILL::Request::Attributes->_resultset->search( -# {}, # No specific conditions -# { -# select => ['type'], -# group_by => ['type'] -# } -# )->get_column('type')->all; - -# for my $type (@distinct_types) { -# my $test = $self->_resultset->_result_class->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 }, -# ); - -# my $schema = $self->_resultset->result_source->schema; - -# $schema->unregister_source('Koha::Schema::Result::Illrequest'); -# $schema->register_class( 'Koha::Schema::Result::Illrequest', $test ); - -# # Testing to check if the dynamically added method has been aded. It's not been!! -# $self->find(1)->extended_attributes_article_title; -# } - -# return map { 'extended_attributes_'.$_ } @distinct_types; -# } +sub _build_extended_attributes_relations { + my ( $self, $types ) = @_; + + my $result_source = $self->_resultset->result_source; + for my $type ( @{$types} ) { + $result_source->add_relationship( + "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_' . $_ } @{$types}; +} =head3 _type diff --git a/Koha/REST/Plugin/Query.pm b/Koha/REST/Plugin/Query.pm index 7927bde4232..d159fef0f67 100644 --- a/Koha/REST/Plugin/Query.pm +++ b/Koha/REST/Plugin/Query.pm @@ -181,9 +181,10 @@ Generates the DBIC join attribute based on extended_attributes query entries, an { my @array = $self->_get_extended_attributes_entries( $filtered_params, 0 ); - foreach my $test (@array) { - push( @{ $attributes->{join} }, 'extended_attributes_'.$test ); - } + # Calling our private method to build the extended attributes relations + my @joins = $result_set->_build_extended_attributes_relations(\@array); + push @{$ attributes->{join} }, @joins; + } } ); -- 2.45.2