Browse Source

Bug 16965: search_related returns an instanciated Koha::Objects-based object

Koha::Objects->search_related should return a Koha::Objects-based
object.
This search_related method should follow the same rules as the search
method, i.e. take into account what the caller want (scalar or list).
The problem here is that we do not know (in Koha::Objects) what is the
kind of objects we want to instanciate. To know it, this patch adds a
get_object_class, it will return the class of the object and the
resultset Koha::Object-based object.

The drawback of this method is that we will have to keep it up-to-date
every time we add a new Koha::Object class.

Signed-off-by: Marc Véron <veron@veron.ch>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
16.11.x
Jonathan Druart 8 years ago
committed by Kyle M Hall
parent
commit
3d110a3c72
  1. 33
      Koha/Objects.pm
  2. 9
      t/db_dependent/Koha/Objects.t

33
Koha/Objects.pm

@ -133,14 +133,29 @@ sub search {
=head3 search_related
my @objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
Searches the specified relationship, optionally specifying a condition and attributes for matching records.
=cut
sub search_related {
my ( $self, @params ) = @_;
return $self->_resultset->search_related( @params );
my ( $self, $rel_name, @params ) = @_;
if (wantarray) {
my @dbic_rows = $self->_resultset()->search_related($rel_name, @params);
my $object_class = get_object_class( $dbic_rows[0]->result_class )->[1];
eval "require $object_class";
return $object_class->_wrap(@dbic_rows);
} else {
my $rs = $self->_resultset()->search_related($rel_name, @params);
my $object_class = get_object_class( $rs->result_class )->[1];
eval "require $object_class";
return $object_class->_new_from_dbic($rs);
}
}
=head3 Koha::Objects->next();
@ -202,7 +217,7 @@ wraps the DBIC object in a corresponding Koha object
sub _wrap {
my ( $self, @dbic_rows ) = @_;
my @objects = map { $self->object_class()->_new_from_dbic( $_ ) } @dbic_rows;
my @objects = map { $self->object_class->_new_from_dbic( $_ ) } @dbic_rows;
return @objects;
}
@ -227,6 +242,18 @@ sub _resultset {
}
}
sub get_object_class {
my ( $type ) = @_;
return unless $type;
$type =~ s|^Koha::Schema::Result::||;
my $mappings = {
Branch => [ qw( Koha::Library Koha::Libraries ) ],
Borrower => [ qw( Koha::Patron Koha::Patrons ) ],
OldIssue => [ qw( Koha::OldIssue Koha::OldIssues ) ],
};
return $mappings->{$type};
}
=head3 columns
my @columns = Koha::Objects->columns

9
t/db_dependent/Koha/Objects.t

@ -82,14 +82,21 @@ subtest 'not_covered_yet' => sub {
};
subtest 'search_related' => sub {
plan tests => 3;
plan tests => 8;
my $builder = t::lib::TestBuilder->new;
my $patron_1 = $builder->build( { source => 'Borrower' } );
my $patron_2 = $builder->build( { source => 'Borrower' } );
my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
is( $libraries->count, 2, 'Koha::Objects->search_related should work as expected' );
is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
is( ref( $libraries[0] ), 'Koha::Library', 'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
is( scalar(@libraries), 2, 'Koha::Objects->search_related should work as expected' );
is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
};
$schema->storage->txn_rollback;

Loading…
Cancel
Save