my @columns = @{$self->_columns()};
foreach my $p ( keys %$properties ) {
- unless ( grep {/^$p$/} @columns ) {
+ unless ( grep { $_ eq $p } @columns ) {
Koha::Exceptions::Object::PropertyNotFound->throw( "No property $p for " . ref($self) );
}
}
sub get_from_storage {
my ( $self, $attrs ) = @_;
my $stored_object = $self->_result->get_from_storage($attrs);
+ return unless $stored_object;
my $object_class = Koha::Object::_get_object_class( $self->_result->result_class );
return $object_class->_new_from_dbic($stored_object);
}
return ( grep { $column_type eq $_ } @numeric_types) ? 1 : 0;
}
+=head3 prefetch_whitelist
+
+ my $whitelist = $object->prefetch_whitelist()
+
+Returns a hash of prefetchable subs and the type they return.
+
+=cut
+
+sub prefetch_whitelist {
+ my ( $self ) = @_;
+
+ my $whitelist = {};
+ my $relations = $self->_result->result_source->_relationships;
+
+ foreach my $key (keys %{$relations}) {
+ if($self->can($key)) {
+ my $result_class = $relations->{$key}->{class};
+ my $obj = $result_class->new;
+ try {
+ $whitelist->{$key} = $obj->koha_object_class;
+ } catch {
+ $whitelist->{$key} = undef;
+ }
+ }
+ }
+
+ return $whitelist;
+}
+
=head3 to_api
my $object_for_api = $object->to_api(
if ($embeds) {
foreach my $embed ( keys %{$embeds} ) {
- my $curr = $embed;
- my $next = $embeds->{$curr}->{children};
-
- my $children = $self->$curr;
+ if ( $embed =~ m/^(?<relation>.*)_count$/
+ and $embeds->{$embed}->{is_count} ) {
- if ( defined $children and ref($children) eq 'ARRAY' ) {
- my @list = map {
- $self->_handle_to_api_child(
- { child => $_, next => $next, curr => $curr } )
- } @{$children};
- $json_object->{$curr} = \@list;
+ my $relation = $+{relation};
+ $json_object->{$embed} = $self->$relation->count;
}
else {
- $json_object->{$curr} = $self->_handle_to_api_child(
- { child => $children, next => $next, curr => $curr } );
+ my $curr = $embed;
+ my $next = $embeds->{$curr}->{children};
+
+ my $children = $self->$curr;
+
+ if ( defined $children and ref($children) eq 'ARRAY' ) {
+ my @list = map {
+ $self->_handle_to_api_child(
+ { child => $_, next => $next, curr => $curr } )
+ } @{$children};
+ $json_object->{$curr} = \@list;
+ }
+ else {
+ $json_object->{$curr} = $self->_handle_to_api_child(
+ { child => $children, next => $next, curr => $curr } );
+ }
}
}
}
? $from_api_mapping->{$key}
: $key;
- if ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) {
+ if ( $columns_info->{$koha_field_name}->{is_boolean} ) {
+ # TODO: Remove when D8 is formally deprecated
+ # Handle booleans gracefully
+ $value = ( $value ) ? 1 : 0;
+ }
+ elsif ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) {
try {
$value = dt_from_string($value, 'rfc3339');
}
my @columns = @{$self->_columns()};
# Using direct setter/getter like $item->barcode() or $item->barcode($barcode);
- if ( grep {/^$method$/} @columns ) {
+ if ( grep { $_ eq $method } @columns ) {
if ( @_ ) {
$self->_result()->set_column( $method, @_ );
return $self;
Koha::Exceptions::Object::MethodNotCoveredByTests->throw(
error => sprintf("The method %s->%s is not covered by tests!", ref($self), $method),
show_trace => 1
- ) unless grep { /^$method$/ } @known_methods;
+ ) unless grep { $_ eq $method } @known_methods;
my $r = eval { $self->_result->$method(@_) };