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

(-)a/Koha/Objects.pm (-32 / +9 lines)
Lines 34-40 Koha::Objects - Koha Object set base class Link Here
34
=head1 SYNOPSIS
34
=head1 SYNOPSIS
35
35
36
    use Koha::Objects;
36
    use Koha::Objects;
37
    my @objects = Koha::Objects->search({ borrowernumber => $borrowernumber});
37
    my $objects = Koha::Objects->search({ borrowernumber => $borrowernumber});
38
38
39
=head1 DESCRIPTION
39
=head1 DESCRIPTION
40
40
Lines 122-129 sub find_or_create { Link Here
122
122
123
=head3 search
123
=head3 search
124
124
125
    # list context
126
    my @objects = Koha::Objects->search([$params, $attributes]);
127
    # scalar context
125
    # scalar context
128
    my $objects = Koha::Objects->search([$params, $attributes]);
126
    my $objects = Koha::Objects->search([$params, $attributes]);
129
    while (my $object = $objects->next) {
127
    while (my $object = $objects->next) {
Lines 133-163 sub find_or_create { Link Here
133
This B<instantiates> the I<Koha::Objects> class, and generates a resultset
131
This B<instantiates> the I<Koha::Objects> class, and generates a resultset
134
based on the query I<$params> and I<$attributes> that are passed (like in DBIC).
132
based on the query I<$params> and I<$attributes> that are passed (like in DBIC).
135
133
136
In B<list context> it returns an array of I<Koha::Object> objects.
137
In B<scalar context> it returns an iterator.
138
139
=cut
134
=cut
140
135
141
sub search {
136
sub search {
142
    my ( $self, $params, $attributes ) = @_;
137
    my ( $self, $params, $attributes ) = @_;
143
138
144
    if (wantarray) {
139
    my $class = ref($self) ? ref($self) : $self;
145
        my @dbic_rows = $self->_resultset()->search($params, $attributes);
140
    my $rs = $self->_resultset()->search($params, $attributes);
146
147
        return $self->_wrap(@dbic_rows);
148
141
149
    }
142
    return $class->_new_from_dbic($rs);
150
    else {
151
        my $class = ref($self) ? ref($self) : $self;
152
        my $rs = $self->_resultset()->search($params, $attributes);
153
154
        return $class->_new_from_dbic($rs);
155
    }
156
}
143
}
157
144
158
=head3 search_related
145
=head3 search_related
159
146
160
    my @objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
161
    my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
147
    my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
162
148
163
Searches the specified relationship, optionally specifying a condition and attributes for matching records.
149
Searches the specified relationship, optionally specifying a condition and attributes for matching records.
Lines 168-189 sub search_related { Link Here
168
    my ( $self, $rel_name, @params ) = @_;
154
    my ( $self, $rel_name, @params ) = @_;
169
155
170
    return if !$rel_name;
156
    return if !$rel_name;
171
    if (wantarray) {
172
        my @dbic_rows = $self->_resultset()->search_related($rel_name, @params);
173
        return if !@dbic_rows;
174
        my $object_class = _get_objects_class( $dbic_rows[0]->result_class );
175
157
176
        eval "require $object_class";
158
    my $rs = $self->_resultset()->search_related($rel_name, @params);
177
        return _wrap( $object_class, @dbic_rows );
159
    return if !$rs;
160
    my $object_class = _get_objects_class( $rs->result_class );
178
161
179
    } else {
162
    eval "require $object_class";
180
        my $rs = $self->_resultset()->search_related($rel_name, @params);
163
    return _new_from_dbic( $object_class, $rs );
181
        return if !$rs;
182
        my $object_class = _get_objects_class( $rs->result_class );
183
184
        eval "require $object_class";
185
        return _new_from_dbic( $object_class, $rs );
186
    }
187
}
164
}
188
165
189
=head3 delete
166
=head3 delete
(-)a/t/db_dependent/Koha/Objects.t (-50 / +5 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 24;
22
use Test::More tests => 23;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Warn;
25
use Test::Warn;
Lines 141-147 subtest 'find' => sub { Link Here
141
};
141
};
142
142
143
subtest 'search_related' => sub {
143
subtest 'search_related' => sub {
144
    plan tests => 6;
144
145
    plan tests => 3;
146
145
    my $builder   = t::lib::TestBuilder->new;
147
    my $builder   = t::lib::TestBuilder->new;
146
    my $patron_1  = $builder->build( { source => 'Borrower' } );
148
    my $patron_1  = $builder->build( { source => 'Borrower' } );
147
    my $patron_2  = $builder->build( { source => 'Borrower' } );
149
    my $patron_2  = $builder->build( { source => 'Borrower' } );
Lines 163-188 subtest 'search_related' => sub { Link Here
163
        [ $patron_1->{branchcode}, $patron_2->{branchcode} ] ),
165
        [ $patron_1->{branchcode}, $patron_2->{branchcode} ] ),
164
        'Koha::Objects->search_related should work as expected'
166
        'Koha::Objects->search_related should work as expected'
165
    );
167
    );
166
167
    my @libraries = Koha::Patrons->search(
168
        {
169
            -or => {
170
                borrowernumber =>
171
                  [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ]
172
            }
173
        }
174
    )->search_related('branchcode');
175
    is(
176
        ref( $libraries[0] ), 'Koha::Library',
177
        'Koha::Objects->search_related should return a list of Koha::Object-based objects'
178
    );
179
    is( scalar(@libraries), 2,
180
        'Koha::Objects->search_related should work as expected' );
181
    ok( eq_array(
182
        [ map { $_->branchcode } @libraries ],
183
        [ $patron_1->{branchcode}, $patron_2->{branchcode} ] ),
184
        'Koha::Objects->search_related should work as expected'
185
    );
186
};
168
};
187
169
188
subtest 'single' => sub {
170
subtest 'single' => sub {
Lines 211-217 subtest 'last' => sub { Link Here
211
193
212
subtest 'get_column' => sub {
194
subtest 'get_column' => sub {
213
    plan tests => 1;
195
    plan tests => 1;
214
    my @cities = Koha::Cities->search;
196
    my @cities = Koha::Cities->search->as_list;
215
    my @city_names = map { $_->city_name } @cities;
197
    my @city_names = map { $_->city_name } @cities;
216
    is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
198
    is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
217
};
199
};
Lines 287-318 subtest '->is_paged and ->pager tests' => sub { Link Here
287
    $schema->storage->txn_rollback;
269
    $schema->storage->txn_rollback;
288
};
270
};
289
271
290
subtest '->search() tests' => sub {
291
292
    plan tests => 12;
293
294
    $schema->storage->txn_begin;
295
296
    my $count = Koha::Patrons->search->count;
297
298
    # Create 10 patrons
299
    foreach (1..10) {
300
        $builder->build_object({ class => 'Koha::Patrons' });
301
    }
302
303
    my $patrons = Koha::Patrons->search();
304
    is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
305
    my @patrons = Koha::Patrons->search();
306
    is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
307
    my $i = 0;
308
    foreach (1..10) {
309
        is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
310
        $i++;
311
    }
312
313
    $schema->storage->txn_rollback;
314
};
315
316
subtest "to_api() tests" => sub {
272
subtest "to_api() tests" => sub {
317
273
318
    plan tests => 19;
274
    plan tests => 19;
319
- 

Return to bug 29844