}
}
+=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();
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/.*:://;
use Modern::Perl;
-use Test::More tests => 10;
+use Test::More tests => 11;
use Test::Warn;
use Koha::Authority::Types;
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;