Browse Source

Only reset contain options, don't clear matching() state.

Refs #7412
Mark Story 10 years ago
parent
commit
5ec0a0ca96
3 changed files with 45 additions and 1 deletions
  1. 15 0
      src/ORM/EagerLoader.php
  2. 1 1
      src/ORM/Query.php
  3. 29 0
      tests/TestCase/ORM/EagerLoaderTest.php

+ 15 - 0
src/ORM/EagerLoader.php

@@ -142,6 +142,21 @@ class EagerLoader
     }
 
     /**
+     * Remove any existing non-matching based containments.
+     *
+     * This will reset/clear out any contained associations that were not
+     * added via matching().
+     *
+     * @return void
+     */
+    public function clearContain()
+    {
+        $this->_containments = [];
+        $this->_normalized = $this->_loadExternal = null;
+        $this->_aliasList = [];
+    }
+
+    /**
      * Set whether or not contained associations will load fields automatically.
      *
      * @param bool $value The value to set.

+ 1 - 1
src/ORM/Query.php

@@ -288,7 +288,7 @@ class Query extends DatabaseQuery implements JsonSerializable
     public function contain($associations = null, $override = false)
     {
         if ($override) {
-            $this->_eagerLoader = null;
+            $this->_eagerLoader->clearContain();
         }
 
         $result = $this->eagerLoader()->contain($associations);

+ 29 - 0
tests/TestCase/ORM/EagerLoaderTest.php

@@ -444,6 +444,35 @@ class EagerLoaderTest extends TestCase
     }
 
     /**
+     * Test clearing containments but not matching joins.
+     *
+     * @return void
+     */
+    public function testClearContain()
+    {
+        $contains = [
+            'clients' => [
+                'orders' => [
+                    'orderTypes',
+                    'stuff' => ['stuffTypes']
+                ],
+                'companies' => [
+                    'categories'
+                ]
+            ]
+        ];
+
+        $loader = new EagerLoader();
+        $loader->contain($contains);
+        $loader->matching('clients.addresses');
+
+        $this->assertNull($loader->clearContain());
+        $result = $loader->normalized($this->table);
+        $this->assertEquals([], $result);
+        $this->assertArrayHasKey('clients', $loader->matching());
+    }
+
+    /**
      * Helper function sued to quoted both keys and values in an array in case
      * the test suite is running with auto quoting enabled
      *