From 1ad30e63ea5e1d574d7cf82e86948ef501920d64 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Thu, 15 Dec 2016 19:28:38 +0200 Subject: [PATCH] Bug 17783: Add Koha::Objects->single Returns one and only one object that is part of this set. Returns undef if there are no objects found. ->single is faster than ->search->next This is optimal as it will grab the first returned result without instantiating a cursor. It is useful for this Bug as we only want to select the top row of found issuing rules. To test: 1. Run t/db_dependent/Koha/Objects.t Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- Koha/Objects.pm | 26 +++++++++++++++++++++++++- t/db_dependent/Koha/Objects.t | 13 ++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 56f165c27d..92312066f8 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -161,6 +161,30 @@ sub search_related { } } +=head3 single + +my $object = Koha::Objects->search({}, { rows => 1 })->single + +Returns one and only one object that is part of this set. +Returns undef if there are no objects found. + +This is optimal as it will grab the first returned result without instantiating +a cursor. + +See: +http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#Retrieve_one_and_only_one_row_from_a_resultset + +=cut + +sub single { + my ($self) = @_; + + my $single = $self->_resultset()->single; + return unless $single; + + return $self->object_class()->_new_from_dbic($single); +} + =head3 Koha::Objects->next(); my $object = Koha::Objects->next(); @@ -299,7 +323,7 @@ Currently count, pager, update and delete are covered. sub AUTOLOAD { my ( $self, @params ) = @_; - my @known_methods = qw( count pager update delete result_class ); + my @known_methods = qw( count pager update delete result_class single ); my $method = our $AUTOLOAD; $method =~ s/.*:://; diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t index 84b1a861c5..29dea679bb 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 => 10; +use Test::More tests => 11; use Test::Warn; use Koha::Authority::Types; @@ -116,6 +116,17 @@ subtest 'search_related' => sub { is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' ); }; +subtest 'single' => sub { + plan tests => 2; + my $builder = t::lib::TestBuilder->new; + my $patron_1 = $builder->build( { source => 'Borrower' } ); + my $patron_2 = $builder->build( { source => 'Borrower' } ); + my $patron = Koha::Patrons->search({}, { rows => 1 })->single; + is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.'); + warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/, + "Warning is presented if single is used for a result with multiple rows."; +}; + subtest 'Exceptions' => sub { plan tests => 2; -- 2.39.5