From 1b9dfc2cc056ac6126a50d9d4ace25c893f03db3 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 18 Jul 2024 14:49:17 +0000 Subject: [PATCH] Bug 37389: PoC Follow-up Its close but it appears the dyncamically added accessors are not working properly, or not being registered properly --- Koha/ILL/Request.pm | 7 +++++++ Koha/ILL/Requests.pm | 14 +++++++++++--- Koha/REST/Plugin/Objects.pm | 3 ++- Koha/REST/Plugin/Query.pm | 25 +++++++++++++++++++------ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Koha/ILL/Request.pm b/Koha/ILL/Request.pm index 24c3a2222f..d8b669fe95 100644 --- a/Koha/ILL/Request.pm +++ b/Koha/ILL/Request.pm @@ -2247,6 +2247,13 @@ sub AUTOLOAD { # Call the newly defined method return $self->$name(); } + my $wt = 'SUPER::'.$name; + return $self->$wt; +} + +sub get_column(){ + my ($self, $column_name) = @_; + return $self->_result->get_column($column_name); } =head3 _type diff --git a/Koha/ILL/Requests.pm b/Koha/ILL/Requests.pm index 8c5c27765f..289ce73d97 100644 --- a/Koha/ILL/Requests.pm +++ b/Koha/ILL/Requests.pm @@ -122,16 +122,16 @@ Returns a list of relation accessor names. sub _build_extended_attributes_relations { my ($self) = @_; - my @distinct_types = $self->_resultset->search( + my @distinct_types = Koha::ILL::Request::Attributes->_resultset->search( {}, # No specific conditions { select => ['type'], - distinct => 1, + group_by => ['type'] } )->get_column('type')->all; for my $type (@distinct_types) { - $self->_resultset->has_many( + my $test = $self->_resultset->_result_class->has_many( "extended_attributes_$type", "Koha::Schema::Result::Illrequestattribute", sub { @@ -144,6 +144,14 @@ sub _build_extended_attributes_relations { }, { 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; diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 7f06582b4d..d14d8c99a4 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -291,7 +291,8 @@ controller, and thus shouldn't be called twice in it. $c->dbic_extended_attributes_join( { attributes => $attributes, - filtered_params => $filtered_params + filtered_params => $filtered_params, + result_set => $result_set } ); diff --git a/Koha/REST/Plugin/Query.pm b/Koha/REST/Plugin/Query.pm index 53decc94bc..b193d45812 100644 --- a/Koha/REST/Plugin/Query.pm +++ b/Koha/REST/Plugin/Query.pm @@ -173,16 +173,29 @@ Generates the DBIC join attribute based on extended_attributes query entries, an my $attributes = $args->{attributes}; my $filtered_params = $args->{filtered_params}; + my $result_set = $args->{result_set}; if ( reftype( $attributes->{prefetch} ) && reftype( $attributes->{prefetch} ) eq 'ARRAY' && grep ( /extended_attributes/, @{ $attributes->{prefetch} } ) ) { - my $ea_entries = $self->_get_extended_attributes_entries( $filtered_params, 0 ); - while ( $ea_entries > 0 ) { - push( @{ $attributes->{join} }, 'extended_attributes' ); - $ea_entries--; + # my $ea_entries = $self->_get_extended_attributes_entries( $filtered_params, 0 ); + + # Calling our private method to build the extended attributes relations + $result_set->_build_extended_attributes_relations(); + + # Testing out the extended attributes join (hardcoded for now) + my @test = qw( extended_attributes_article_title extended_attributes_issue extended_attributes_volume ); + foreach my $test (@test) { + push( @{ $attributes->{join} }, $test ); } + + # while ( $ea_entries > 0 ) { + # push( @{ $attributes->{join} }, 'extended_attributes_title' ); + # push( @{ $attributes->{join} }, 'extended_attributes_author' ); + # $ea_entries--; + # } + } } ); @@ -630,11 +643,11 @@ sub _rewrite_related_metadata_query { $extended_attributes_entries++; if ( $extended_attributes_entries > 1 ) { my $old_key_value = delete $params->{ 'extended_attributes.' . $key }; - my $new_key_value = "extended_attributes_$extended_attributes_entries" . "." . $key; + my $new_key_value = "extended_attributes_$key" . "." . $key; $params->{$new_key_value} = $old_key_value; my $old_value_value = delete $params->{ 'extended_attributes.' . $value }; - my $new_value_value = "extended_attributes_$extended_attributes_entries" . "." . $value; + my $new_value_value = "extended_attributes_$key" . "." . $value; $params->{$new_value_value} = $old_value_value; } -- 2.39.2