View | Details | Raw Unified | Return to bug 17783
Collapse All | Expand All

(-)a/Koha/Objects.pm (-1 / +25 lines)
Lines 161-166 sub search_related { Link Here
161
    }
161
    }
162
}
162
}
163
163
164
=head3 single
165
166
my $object = Koha::Objects->search({}, { rows => 1 })->single
167
168
Returns one and only one object that is part of this set.
169
Returns undef if there are no objects found.
170
171
This is optimal as it will grab the first returned result without instantiating
172
a cursor.
173
174
See:
175
http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#Retrieve_one_and_only_one_row_from_a_resultset
176
177
=cut
178
179
sub single {
180
    my ($self) = @_;
181
182
    my $single = $self->_resultset()->single;
183
    return unless $single;
184
185
    return $self->object_class()->_new_from_dbic($single);
186
}
187
164
=head3 Koha::Objects->next();
188
=head3 Koha::Objects->next();
165
189
166
my $object = Koha::Objects->next();
190
my $object = Koha::Objects->next();
Lines 299-305 Currently count, pager, update and delete are covered. Link Here
299
sub AUTOLOAD {
323
sub AUTOLOAD {
300
    my ( $self, @params ) = @_;
324
    my ( $self, @params ) = @_;
301
325
302
    my @known_methods = qw( count pager update delete result_class );
326
    my @known_methods = qw( count pager update delete result_class single );
303
    my $method = our $AUTOLOAD;
327
    my $method = our $AUTOLOAD;
304
    $method =~ s/.*:://;
328
    $method =~ s/.*:://;
305
329
(-)a/t/db_dependent/Koha/Objects.t (-2 / +12 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 10;
22
use Test::More tests => 11;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use Koha::Authority::Types;
25
use Koha::Authority::Types;
Lines 116-121 subtest 'search_related' => sub { Link Here
116
    is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
116
    is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
117
};
117
};
118
118
119
subtest 'single' => sub {
120
    plan tests => 2;
121
    my $builder   = t::lib::TestBuilder->new;
122
    my $patron_1  = $builder->build( { source => 'Borrower' } );
123
    my $patron_2  = $builder->build( { source => 'Borrower' } );
124
    my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
125
    is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
126
    warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
127
    "Warning is presented if single is used for a result with multiple rows.";
128
};
129
119
subtest 'Exceptions' => sub {
130
subtest 'Exceptions' => sub {
120
    plan tests => 2;
131
    plan tests => 2;
121
132
122
- 

Return to bug 17783