From 9a4c6285958d0d7ac913946202a9836f0238eb53 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 22 Jul 2016 17:20:08 +0100 Subject: [PATCH] Bug 16965: Add Koha::Objects->search_related MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In order to search on relations, we need this new method. Test plan: Confirm that the changes in Objects.t make sense and that the tests pass. Tested all 3 patches together, followed test plan, result OK Signed-off-by: Marc VĂ©ron --- Koha/Objects.pm | 13 +++++++++++++ t/db_dependent/Koha/Objects.t | 13 ++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 97436ad..26dec34 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -130,6 +130,19 @@ sub search { } } +=head3 search_related + + 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 ); +} + =head3 Koha::Objects->count(); my @objects = Koha::Objects->count($params); diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t index 3551caa..c20fe6c 100644 --- a/t/db_dependent/Koha/Objects.t +++ b/t/db_dependent/Koha/Objects.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Koha::Authority::Types; use Koha::Cities; @@ -51,5 +51,16 @@ subtest 'update' => sub { is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' ); }; +subtest 'search_related' => sub { + plan tests => 3; + 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( $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' ); +}; + $schema->storage->txn_rollback; 1; -- 2.1.4