From a71d636cfaf670f8ac929f8744a835b63dc964bd Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 25 Jul 2016 11:16:59 +0100 Subject: [PATCH] Bug 16965: search_related returns an instanciated Koha::Objects-based object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Tomas Cohen Arazi --- Koha/Objects.pm | 33 ++++++++++++++++++++++++++++++--- t/db_dependent/Koha/Objects.t | 9 ++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 26dec34..bb8629c 100644 --- a/Koha/Objects.pm +++ b/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->count(); @@ -242,7 +257,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; } @@ -280,6 +295,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 diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t index c20fe6c..3996bdd 100644 --- a/t/db_dependent/Koha/Objects.t +++ b/t/db_dependent/Koha/Objects.t @@ -52,14 +52,21 @@ subtest 'update' => 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; -- 2.10.1