@@ -, +, @@ Koha::Objects-based object --- Koha/Objects.pm | 33 ++++++++++++++++++++++++++++++--- t/db_dependent/Koha/Objects.t | 9 ++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) --- a/Koha/Objects.pm +++ a/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 --- a/t/db_dependent/Koha/Objects.t +++ a/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; --