Bug 24356: Make objects.search prefetch embedded relations
[koha-ffzg.git] / Koha / Object.pm
index 4725f23..55d9638 100644 (file)
@@ -253,7 +253,7 @@ sub set {
     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) );
         }
     }
@@ -280,6 +280,7 @@ sub unblessed {
 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);
 }
@@ -372,6 +373,35 @@ sub _numeric_column_type {
     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(
@@ -428,21 +458,29 @@ sub 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 } );
+                }
             }
         }
     }
@@ -545,7 +583,12 @@ sub attributes_from_api {
           ? $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');
             }
@@ -647,7 +690,7 @@ sub AUTOLOAD {
 
     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;
@@ -662,7 +705,7 @@ sub AUTOLOAD {
     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(@_) };