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 <benjamin.rokseth@deichman.no>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2018-01-08 13:19:16 -03:00
parent 28c39d5c86
commit b5bbda68fa
1 changed files with 44 additions and 1 deletions

View File

@ -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(@_) };