Browse Source

Allow any contain call to overwrite the eagerloader.

The override parameter should always work when used.

Refs #7412
Mark Story 10 years ago
parent
commit
b417193255
2 changed files with 23 additions and 1 deletions
  1. 1 1
      src/ORM/Query.php
  2. 22 0
      tests/TestCase/ORM/QueryTest.php

+ 1 - 1
src/ORM/Query.php

@@ -287,7 +287,7 @@ class Query extends DatabaseQuery implements JsonSerializable
      */
     public function contain($associations = null, $override = false)
     {
-        if (empty($associations) && $override) {
+        if ($override) {
             $this->_eagerLoader = null;
         }
 

+ 22 - 0
tests/TestCase/ORM/QueryTest.php

@@ -1823,6 +1823,28 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test overwriting the contained associations.
+     *
+     * @return void
+     */
+    public function testContainOverwrite()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->hasMany('Comments');
+        $table->belongsTo('Authors');
+
+        $query = $table->find();
+        $query->contain(['Comments']);
+        $this->assertEquals(['Comments'], array_keys($query->contain()));
+
+        $query->contain(['Authors'], true);
+        $this->assertEquals(['Authors'], array_keys($query->contain()));
+
+        $query->contain(['Comments', 'Authors'], true);
+        $this->assertEquals(['Comments', 'Authors'], array_keys($query->contain()));
+    }
+
+    /**
      * Integration test to show filtering associations using contain and a closure
      *
      * @return void