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

(-)a/C4/Letters.pm (-3 / +3 lines)
Lines 1575-1583 sub _get_tt_params { Link Here
1575
                        foreach my $key ( @$fk ) {
1575
                        foreach my $key ( @$fk ) {
1576
                            $search->{$key} = $id->{$key};
1576
                            $search->{$key} = $id->{$key};
1577
                        }
1577
                        }
1578
                        $object = $module->search( $search )->next();
1578
                        $object = $module->search( $search )->last;
1579
                    } else { # Foreign key is single column
1579
                    } else { # Foreign key is single column
1580
                        $object = $module->search( { $fk => $id } )->next();
1580
                        $object = $module->search( { $fk => $id } )->last;
1581
                    }
1581
                    }
1582
                } else { # using the table's primary key for lookup
1582
                } else { # using the table's primary key for lookup
1583
                    $object = $module->find($id);
1583
                    $object = $module->find($id);
Lines 1587-1593 sub _get_tt_params { Link Here
1587
            else {    # $ref eq 'ARRAY'
1587
            else {    # $ref eq 'ARRAY'
1588
                my $object;
1588
                my $object;
1589
                if ( @{ $tables->{$table} } == 1 ) {    # Param is a single key
1589
                if ( @{ $tables->{$table} } == 1 ) {    # Param is a single key
1590
                    $object = $module->search( { $pk => $tables->{$table} } )->next();
1590
                    $object = $module->search( { $pk => $tables->{$table} } )->last;
1591
                }
1591
                }
1592
                else {                                  # Params are mutliple foreign keys
1592
                else {                                  # Params are mutliple foreign keys
1593
                    croak "Multiple foreign keys (table $table) should be passed using an hashref";
1593
                    croak "Multiple foreign keys (table $table) should be passed using an hashref";
(-)a/Koha/Objects.pm (-1 / +25 lines)
Lines 205-210 sub next { Link Here
205
    return $object;
205
    return $object;
206
}
206
}
207
207
208
=head3 Koha::Objects->last;
209
210
my $object = Koha::Objects->last;
211
212
Returns the last object that is part of this set.
213
Returns undef if there are no object to return.
214
215
=cut
216
217
sub last {
218
    my ( $self ) = @_;
219
220
    my $count = $self->_resultset->count;
221
    return unless $count;
222
223
    my $result = $self->_resultset->slice($count - 1, $count)->first;
224
225
    my $object = $self->object_class()->_new_from_dbic( $result );
226
227
    return $object;
228
}
229
230
231
208
=head3 Koha::Objects->reset();
232
=head3 Koha::Objects->reset();
209
233
210
Koha::Objects->reset();
234
Koha::Objects->reset();
Lines 335-341 Currently count, pager, update and delete are covered. Link Here
335
sub AUTOLOAD {
359
sub AUTOLOAD {
336
    my ( $self, @params ) = @_;
360
    my ( $self, @params ) = @_;
337
361
338
    my @known_methods = qw( count pager update delete result_class single );
362
    my @known_methods = qw( count pager update delete result_class single slice );
339
    my $method = our $AUTOLOAD;
363
    my $method = our $AUTOLOAD;
340
    $method =~ s/.*:://;
364
    $method =~ s/.*:://;
341
365
(-)a/t/db_dependent/Koha/Objects.t (-2 / +14 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 12;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use Koha::Authority::Types;
25
use Koha::Authority::Types;
Lines 127-132 subtest 'single' => sub { Link Here
127
    "Warning is presented if single is used for a result with multiple rows.";
127
    "Warning is presented if single is used for a result with multiple rows.";
128
};
128
};
129
129
130
subtest 'last' => sub {
131
    plan tests => 3;
132
    my $builder = t::lib::TestBuilder->new;
133
    my $patron_1  = $builder->build( { source => 'Borrower' } );
134
    my $patron_2  = $builder->build( { source => 'Borrower' } );
135
    my $last_patron = Koha::Patrons->search->last;
136
    is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
137
    $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
138
    is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
139
    $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
140
    is( $last_patron, undef, '->last should return undef if search does not return any results' );
141
};
142
130
subtest 'Exceptions' => sub {
143
subtest 'Exceptions' => sub {
131
    plan tests => 2;
144
    plan tests => 2;
132
145
133
- 

Return to bug 18332