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

(-)a/Koha/Objects.pm (-3 / +30 lines)
Lines 133-146 sub search { Link Here
133
=head3 search_related
133
=head3 search_related
134
134
135
    my @objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
135
    my @objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
136
    my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
136
137
137
Searches the specified relationship, optionally specifying a condition and attributes for matching records.
138
Searches the specified relationship, optionally specifying a condition and attributes for matching records.
138
139
139
=cut
140
=cut
140
141
141
sub search_related {
142
sub search_related {
142
    my ( $self, @params ) = @_;
143
    my ( $self, $rel_name, @params ) = @_;
143
    return $self->_resultset->search_related( @params );
144
145
    if (wantarray) {
146
        my @dbic_rows = $self->_resultset()->search_related($rel_name, @params);
147
        my $object_class = get_object_class( $dbic_rows[0]->result_class )->[1];
148
149
        eval "require $object_class";
150
        return $object_class->_wrap(@dbic_rows);
151
152
    } else {
153
        my $rs = $self->_resultset()->search_related($rel_name, @params);
154
        my $object_class = get_object_class( $rs->result_class )->[1];
155
156
        eval "require $object_class";
157
        return $object_class->_new_from_dbic($rs);
158
    }
144
}
159
}
145
160
146
=head3 Koha::Objects->count();
161
=head3 Koha::Objects->count();
Lines 242-248 wraps the DBIC object in a corresponding Koha object Link Here
242
sub _wrap {
257
sub _wrap {
243
    my ( $self, @dbic_rows ) = @_;
258
    my ( $self, @dbic_rows ) = @_;
244
259
245
    my @objects = map { $self->object_class()->_new_from_dbic( $_ ) } @dbic_rows;
260
    my @objects = map { $self->object_class->_new_from_dbic( $_ ) } @dbic_rows;
246
261
247
    return @objects;
262
    return @objects;
248
}
263
}
Lines 280-285 sub _resultset { Link Here
280
    }
295
    }
281
}
296
}
282
297
298
sub get_object_class {
299
    my ( $type ) = @_;
300
    return unless $type;
301
    $type =~ s|^Koha::Schema::Result::||;
302
    my $mappings = {
303
        Branch => [ qw( Koha::Library Koha::Libraries ) ],
304
        Borrower => [ qw( Koha::Patron Koha::Patrons ) ],
305
        OldIssue => [ qw( Koha::OldIssue Koha::OldIssues ) ],
306
    };
307
    return $mappings->{$type};
308
}
309
283
=head3 columns
310
=head3 columns
284
311
285
my @columns = Koha::Objects->columns
312
my @columns = Koha::Objects->columns
(-)a/t/db_dependent/Koha/Objects.t (-2 / +8 lines)
Lines 52-65 subtest 'update' => sub { Link Here
52
};
52
};
53
53
54
subtest 'search_related' => sub {
54
subtest 'search_related' => sub {
55
    plan tests => 3;
55
    plan tests => 8;
56
    my $builder   = t::lib::TestBuilder->new;
56
    my $builder   = t::lib::TestBuilder->new;
57
    my $patron_1  = $builder->build( { source => 'Borrower' } );
57
    my $patron_1  = $builder->build( { source => 'Borrower' } );
58
    my $patron_2  = $builder->build( { source => 'Borrower' } );
58
    my $patron_2  = $builder->build( { source => 'Borrower' } );
59
    my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
59
    my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
60
    is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
60
    is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
61
    is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
61
    is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
62
    is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
62
    is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
63
    is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
64
65
    my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
66
    is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
67
    is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
68
    is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
69
    is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
63
};
70
};
64
71
65
$schema->storage->txn_rollback;
72
$schema->storage->txn_rollback;
66
- 

Return to bug 16965