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

(-)a/Koha/Object.pm (+24 lines)
Lines 372-377 sub _numeric_column_type { Link Here
372
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
372
    return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
373
}
373
}
374
374
375
=head3 prefetch_whitelist
376
    
377
    my $whitelist = $object->prefetch_whitelist()
378
379
Returns a hash of prefetchable subs and the type they return.
380
381
=cut
382
383
sub prefetch_whitelist {
384
    my ( $self ) = @_;
385
386
    my $whitelist = {};
387
    my $relations = $self->_result->result_source->_relationships;
388
    foreach my $key (keys %{$relations}) {
389
        if($self->can($key)) {
390
            my $result_class = $relations->{$key}->{class};
391
            my $obj = $result_class->new;
392
            $whitelist->{$key} = $obj->koha_object_class if $obj->can('koha_object_class');
393
        }
394
    }
395
396
    return $whitelist;
397
}
398
375
=head3 to_api
399
=head3 to_api
376
400
377
    my $object_for_api = $object->to_api(
401
    my $object_for_api = $object->to_api(
(-)a/Koha/Objects.pm (+16 lines)
Lines 350-355 sub from_api_mapping { Link Here
350
    return $self->{_singular_object}->from_api_mapping;
350
    return $self->{_singular_object}->from_api_mapping;
351
}
351
}
352
352
353
=head3 prefetch_whitelist
354
    
355
    my $whitelist = $object->prefetch_whitelist()
356
357
Returns a hash of prefetchable subs and the type it returns
358
359
=cut
360
361
sub prefetch_whitelist {
362
    my ( $self ) = @_;
363
364
    $self->{_singular_object} ||= $self->object_class->new();
365
366
    $self->{_singular_object}->prefetch_whitelist;
367
}
368
353
=head3 Koha::Objects->_wrap
369
=head3 Koha::Objects->_wrap
354
370
355
wraps the DBIC object in a corresponding Koha object
371
wraps the DBIC object in a corresponding Koha object
(-)a/Koha/REST/Plugin/Objects.pm (+8 lines)
Lines 71-76 sub register { Link Here
71
                }
71
                }
72
            );
72
            );
73
73
74
            # Generate prefetches for embedded stuff
75
            $c->dbic_merge_prefetch(
76
                {
77
                    attributes => $attributes,
78
                    result_set => $result_set
79
                }
80
            )
81
74
            # Call the to_model function by reference, if defined
82
            # Call the to_model function by reference, if defined
75
            if ( defined $filtered_params ) {
83
            if ( defined $filtered_params ) {
76
84
(-)a/Koha/REST/Plugin/Query.pm (-1 / +49 lines)
Lines 100-105 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$a Link Here
100
        }
100
        }
101
    );
101
    );
102
102
103
=head3 dbic_merge_prefetch
104
105
    $attributes = $c->dbic_merge_prefetch({ attributes => $attributes, result_set => $result_set });
106
107
Generates the DBIC prefetch attribute based on embedded relations, and merges into I<$attributes>.
108
109
=cut
110
111
    $app->helper(
112
        'dbic_merge_prefetch' => sub {
113
            my ( $c, $args ) = @_;
114
            my $attributes = $args->{attributes};
115
            my $result_set = $args->{result_set};
116
            my $embed = $c->stash('koha.embed');
117
118
            return unless defined $embed;
119
120
            my @prefetches;
121
            foreach my $key (keys %{$embed}) {
122
                my $parsed = _parse_prefetch($key, $embed, $result_set);
123
                push @prefetches, $parsed if defined $parsed;
124
            }
125
126
            if(scalar(@prefetches)) {
127
                $attributes->{prefetch} = \@prefetches;
128
            }
129
        }
130
    );
131
103
=head3 _build_query_params_from_api
132
=head3 _build_query_params_from_api
104
133
105
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
134
    my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
Lines 287-290 sub _merge_embed { Link Here
287
    }
316
    }
288
}
317
}
289
318
319
sub _parse_prefetch {
320
    my ( $key, $embed, $result_set) = @_;
321
322
    my $ko_class = $result_set->prefetch_whitelist->{$key};
323
324
    return undef unless defined $ko_class;
325
326
    return $key unless defined $embed->{$key}->{children};
327
328
    my $prefetch = {};
329
    foreach my $child (keys %{$embed->{$key}->{children}}) {
330
        my $parsed = _parse_prefetch($child, $embed->{$key}->{children}, $ko_class->new);
331
        $prefetch->{$child} = $parsed if defined $parsed;
332
    }
333
334
    return undef unless scalar(keys %{$prefetch});
335
336
    return $prefetch;
337
}
338
290
1;
339
1;
291
- 

Return to bug 24356