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->next();
161
=head3 Koha::Objects->next();
Lines 202-208 wraps the DBIC object in a corresponding Koha object Link Here
202
sub _wrap {
217
sub _wrap {
203
    my ( $self, @dbic_rows ) = @_;
218
    my ( $self, @dbic_rows ) = @_;
204
219
205
    my @objects = map { $self->object_class()->_new_from_dbic( $_ ) } @dbic_rows;
220
    my @objects = map { $self->object_class->_new_from_dbic( $_ ) } @dbic_rows;
206
221
207
    return @objects;
222
    return @objects;
208
}
223
}
Lines 227-232 sub _resultset { Link Here
227
    }
242
    }
228
}
243
}
229
244
245
sub get_object_class {
246
    my ( $type ) = @_;
247
    return unless $type;
248
    $type =~ s|^Koha::Schema::Result::||;
249
    my $mappings = {
250
        Branch => [ qw( Koha::Library Koha::Libraries ) ],
251
        Borrower => [ qw( Koha::Patron Koha::Patrons ) ],
252
        OldIssue => [ qw( Koha::OldIssue Koha::OldIssues ) ],
253
    };
254
    return $mappings->{$type};
255
}
256
230
=head3 columns
257
=head3 columns
231
258
232
my @columns = Koha::Objects->columns
259
my @columns = Koha::Objects->columns
(-)a/t/db_dependent/Koha/Objects.t (-2 / +8 lines)
Lines 82-95 subtest 'not_covered_yet' => sub { Link Here
82
};
82
};
83
83
84
subtest 'search_related' => sub {
84
subtest 'search_related' => sub {
85
    plan tests => 3;
85
    plan tests => 8;
86
    my $builder   = t::lib::TestBuilder->new;
86
    my $builder   = t::lib::TestBuilder->new;
87
    my $patron_1  = $builder->build( { source => 'Borrower' } );
87
    my $patron_1  = $builder->build( { source => 'Borrower' } );
88
    my $patron_2  = $builder->build( { source => 'Borrower' } );
88
    my $patron_2  = $builder->build( { source => 'Borrower' } );
89
    my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
89
    my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
90
    is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
90
    is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
91
    is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
91
    is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
92
    is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
92
    is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
93
    is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
94
95
    my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
96
    is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
97
    is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
98
    is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
99
    is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
93
};
100
};
94
101
95
$schema->storage->txn_rollback;
102
$schema->storage->txn_rollback;
96
- 

Return to bug 16965