diff --git a/Cake/ORM/Association/BelongsToMany.php b/Cake/ORM/Association/BelongsToMany.php index 726705c1c2f..b9cc15e561b 100644 --- a/Cake/ORM/Association/BelongsToMany.php +++ b/Cake/ORM/Association/BelongsToMany.php @@ -247,12 +247,18 @@ public function eagerLoader(array $options) { 'foreignKey' => $this->foreignKey(), 'conditions' => [], 'sort' => $this->sort(), - 'strategy' => $this->strategy() + 'strategy' => $this->strategy(), ]; - $fetchQuery = $this->_buildQuery($options); + $queryBuilder = false; if (!empty($options['queryBuilder'])) { - $fetchQuery = $options['queryBuilder']($fetchQuery); + $queryBuilder = $options['queryBuilder']; + unset($options['queryBuilder']); + } + + $fetchQuery = $this->_buildQuery($options); + if ($queryBuilder) { + $fetchQuery = $queryBuilder($fetchQuery); } $resultMap = []; diff --git a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php index 8fb28623fe1..7476189a49f 100644 --- a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php @@ -683,6 +683,71 @@ public function testEagerLoaderSubquery() { $this->assertEquals($row, $result); } +/** + * Tests eagerLoader with queryBuilder + * + * @return void + */ + public function testEagerLoaderWithQueryBuilder() { + $config = [ + 'sourceTable' => $this->article, + 'targetTable' => $this->tag, + ]; + TableRegistry::get('ArticlesTags', [ + 'table' => 'articles_tags', + 'schema' => [ + 'article_id' => ['type' => 'integer'], + 'tag_id' => ['type' => 'integer'] + ] + ]); + $association = new BelongsToMany('Tags', $config); + $keys = [1, 2, 3, 4]; + $query = $this->getMock( + 'Cake\ORM\Query', + ['all', 'contain', 'andWhere', 'limit'], + [null, null] + ); + + $this->tag->expects($this->once()) + ->method('find') + ->with('all') + ->will($this->returnValue($query)); + + $results = [ + ['id' => 1, 'name' => 'foo', 'articles_tags' => ['article_id' => 1]], + ['id' => 2, 'name' => 'bar', 'articles_tags' => ['article_id' => 2]] + ]; + $query->expects($this->once()) + ->method('all') + ->will($this->returnValue($results)); + + $query->expects($this->once())->method('contain') + ->with([ + 'ArticlesTags' => [ + 'conditions' => ['ArticlesTags.article_id in' => $keys], + 'matching' => true + ] + ]) + ->will($this->returnSelf()); + + $query->hydrate(false); + + $query->expects($this->once()) + ->method('andWhere') + ->with(['foo' => 1]) + ->will($this->returnSelf()); + + $query->expects($this->once()) + ->method('limit') + ->with(1) + ->will($this->returnSelf()); + + $queryBuilder = function($q) { + return $q->andWhere(['foo' => 1])->limit(1); + }; + $association->eagerLoader(compact('keys', 'query', 'queryBuilder')); + } + /** * Test cascading deletes. * diff --git a/Cake/Test/TestCase/ORM/Association/HasManyTest.php b/Cake/Test/TestCase/ORM/Association/HasManyTest.php index 9ed818c64d2..a911b241a00 100644 --- a/Cake/Test/TestCase/ORM/Association/HasManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/HasManyTest.php @@ -411,7 +411,7 @@ public function testEagerLoaderWithQueryBuilder() { return $query->select(['a', 'b'])->join('foo')->where(['a' => 1]); }; - $callable = $association->eagerLoader(compact('keys', 'query', 'queryBuilder')); + $association->eagerLoader(compact('keys', 'query', 'queryBuilder')); } /**