Browse Source

Merge pull request #10500 from cleptric/egaerloaded-getter-setter

Split QueryTrait::eagerLoaded() into getter/setter
Mark Story 9 years ago
parent
commit
60142f3de0
3 changed files with 40 additions and 1 deletions
  1. 11 0
      src/Datasource/QueryTrait.php
  2. 1 1
      src/ORM/Query.php
  3. 28 0
      tests/TestCase/ORM/QueryTest.php

+ 11 - 0
src/Datasource/QueryTrait.php

@@ -181,9 +181,20 @@ trait QueryTrait
     }
 
     /**
+     * Returns the current configured query `_eagerLoaded` value
+     *
+     * @return bool
+     */
+    public function isEagerLoaded()
+    {
+        return $this->_eagerLoaded;
+    }
+
+    /**
      * Sets the query instance to be an eager loaded query. If no argument is
      * passed, the current configured query `_eagerLoaded` value is returned.
      *
+     * @deprecated 3.5.0 Use isEagerLoaded() for the getter part instead.
      * @param bool|null $value Whether or not to eager load.
      * @return $this|\Cake\ORM\Query
      */

+ 1 - 1
src/ORM/Query.php

@@ -974,7 +974,7 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
             $table->dispatchEvent('Model.beforeFind', [
                 $this,
                 new ArrayObject($this->_options),
-                !$this->eagerLoaded()
+                !$this->isEagerLoaded()
             ]);
         }
     }

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

@@ -2527,6 +2527,34 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Tests that the isEagerLoaded function works and is transmitted correctly to eagerly
+     * loaded associations
+     *
+     * @return void
+     */
+    public function testIsEagerLoaded()
+    {
+        $table = TableRegistry::get('authors');
+        $table->hasMany('articles');
+        $query = $table->find()->contain(['articles' => function ($q) {
+            $this->assertTrue($q->isEagerLoaded());
+
+            return $q;
+        }]);
+        $this->assertFalse($query->isEagerLoaded());
+
+        $table->eventManager()->attach(function ($e, $q, $o, $primary) {
+            $this->assertTrue($primary);
+        }, 'Model.beforeFind');
+
+        TableRegistry::get('articles')
+            ->eventManager()->attach(function ($e, $q, $o, $primary) {
+                $this->assertFalse($primary);
+            }, 'Model.beforeFind');
+        $query->all();
+    }
+
+    /**
      * Tests that columns from manual joins are also contained in the result set
      *
      * @return void