From b5bbda68fa7d73469f625d40ccee346f48db40bd Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 8 Jan 2018 13:19:16 -0300 Subject: [PATCH] Bug 19926: Add the Koha::Object->unblessed_all_relateds method In order to refactor our legacy code step-by-step, we will need to provide code with the exact same behaviors and then improve it. The same idea appears for the TT syntax for notices: we will want to deprecate the existing syntax in order to support only one syntax. Currently we fetch all the values from the related tables, without knowing which are actually used. I am suggestion to introduce a Koha::Object->unblessed_all_relateds method which will return a hash containing all the fields from the related tables (with the problems we know: collision in column names). It is the existing behavior of GetOverduesForPatron and GetPendingIssues for instance, they are used to send notices and so we have to provide all the data needed. See dependent bugs to understand the context and test this patch. Signed-off-by: Benjamin Rokseth Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart --- Koha/Object.pm | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 7f36a7dc15..00e0c243a8 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -293,6 +293,37 @@ sub _numeric_column_type { return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0; } +=head3 $object->unblessed_all_relateds + +my $everything_into_one_hashref = $object->unblessed_all_relateds + +The unblessed method only retrieves column' values for the column of the object. +In a *few* cases we want to retrieve the information of all the prefetched data. + +=cut + +sub unblessed_all_relateds { + my ($self) = @_; + + my %data; + my $related_resultsets = $self->_result->{related_resultsets} || {}; + my $rs = $self; + while ( $related_resultsets and %$related_resultsets ) { + my @relations = keys %{ $related_resultsets }; + if ( @relations ) { + my $relation = $relations[0]; + $rs = $rs->related_resultset($relation)->get_cache; + $rs = $rs->[0]; # Does it makes sense to have several values here? + my $object_class = Koha::Object::_get_object_class( $rs->result_class ); + my $koha_object = $object_class->_new_from_dbic( $rs ); + $related_resultsets = $rs->{related_resultsets}; + %data = ( %data, %{ $koha_object->unblessed } ); + } + } + %data = ( %data, %{ $self->unblessed } ); + return \%data; +} + =head3 $object->_result(); Returns the internal DBIC Row object @@ -324,6 +355,17 @@ sub _columns { return $self->{_columns}; } +sub _get_object_class { + my ( $type ) = @_; + return unless $type; + + if( $type->can('koha_object_class') ) { + return $type->koha_object_class; + } + $type =~ s|Schema::Result::||; + return ${type}; +} + =head3 AUTOLOAD The autoload method is used only to get and set values for an objects properties. @@ -348,7 +390,8 @@ sub AUTOLOAD { } } - my @known_methods = qw( is_changed id in_storage get_column discard_changes update ); + my @known_methods = qw( is_changed id in_storage get_column discard_changes update related_resultset ); + Koha::Exceptions::Object::MethodNotCoveredByTests->throw( "The method $method is not covered by tests!" ) unless grep {/^$method$/} @known_methods; my $r = eval { $self->_result->$method(@_) }; -- 2.39.5