Browse Source

Merge pull request #14134 from cakephp/optimize-association-format

Don't map() when we don't need to.
Mark Story 6 years ago
parent
commit
00f5b64dad
1 changed files with 8 additions and 7 deletions
  1. 8 7
      src/ORM/Association.php

+ 8 - 7
src/ORM/Association.php

@@ -1264,7 +1264,7 @@ abstract class Association
 
         $property = $options['propertyPath'];
         $propertyPath = explode('.', $property);
-        $query->formatResults(function ($results) use ($formatters, $property, $propertyPath) {
+        $query->formatResults(function ($results) use ($formatters, $property, $propertyPath, $query) {
             $extracted = [];
             foreach ($results as $result) {
                 foreach ($propertyPath as $propertyPathItem) {
@@ -1282,15 +1282,16 @@ abstract class Association
             }
 
             /** @var \Cake\Collection\CollectionInterface $results */
-            return $results
-                ->insert($property, $extracted)
-                ->map(function ($result) {
-                    if ($result instanceof EntityInterface) {
-                        $result->clean();
-                    }
+            $results = $results->insert($property, $extracted);
+            if ($query->isHydrationEnabled()) {
+                $results = $results->map(function ($result) {
+                    $result->clean();
 
                     return $result;
                 });
+            }
+
+            return $results;
         }, Query::PREPEND);
     }