From 852b7099458efdf63a41e858d1d535709ce64677 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 31 May 2013 00:03:44 +0200 Subject: [PATCH] Renaming filtering -> matching as it better expresses the purpose. Adding a unit test for it --- lib/Cake/ORM/Association/BelongsToMany.php | 2 +- .../Association/ExternalAssociationTrait.php | 4 +-- lib/Cake/ORM/Query.php | 2 +- .../ORM/Association/BelongsToManyTest.php | 8 ++--- lib/Cake/Test/TestCase/ORM/QueryTest.php | 36 +++++++++++++++++++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/Cake/ORM/Association/BelongsToMany.php b/lib/Cake/ORM/Association/BelongsToMany.php index 80dac911c03..8ca7c9439fb 100644 --- a/lib/Cake/ORM/Association/BelongsToMany.php +++ b/lib/Cake/ORM/Association/BelongsToMany.php @@ -215,7 +215,7 @@ protected function _buildQuery($options) { $options['contain'][$pivotAlias] = [ 'conditions' => [$key . ' in' => $filter], - 'filtering' => true + 'matching' => true ]; if (!empty($options['fields'])) { diff --git a/lib/Cake/ORM/Association/ExternalAssociationTrait.php b/lib/Cake/ORM/Association/ExternalAssociationTrait.php index b7f326278ef..e3da3922a5e 100644 --- a/lib/Cake/ORM/Association/ExternalAssociationTrait.php +++ b/lib/Cake/ORM/Association/ExternalAssociationTrait.php @@ -36,11 +36,11 @@ trait ExternalAssociationTrait { * Whether this association can be expressed directly in a query join * * @param array $options custom options key that could alter the return value - * @return boolean if the 'filtering' key in $option is true then this function + * @return boolean if the 'matching' key in $option is true then this function * will return true, false otherwise */ public function canBeJoined($options = []) { - return !empty($options['filtering']); + return !empty($options['matching']); } /** diff --git a/lib/Cake/ORM/Query.php b/lib/Cake/ORM/Query.php index 6471f3cd957..ad840a3c089 100644 --- a/lib/Cake/ORM/Query.php +++ b/lib/Cake/ORM/Query.php @@ -185,7 +185,7 @@ protected function _normalizeContain(Table $parent, $alias, $options) { 'conditions' => 1, 'fields' => 1, 'sort' => 1, - 'filtering' => 1 + 'matching' => 1 ]; $instance = $parent->association($alias); diff --git a/lib/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php b/lib/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php index 4df9cc3c08a..0df0ca556f6 100644 --- a/lib/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php +++ b/lib/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php @@ -272,7 +272,7 @@ public function testEagerLoader() { $query->expects($this->once())->method('contain')->with([ 'ArticleTag' => [ 'conditions' => ['ArticleTag.article_id in' => $keys], - 'filtering' => true + 'matching' => true ] ]); @@ -327,7 +327,7 @@ public function testEagerLoaderWithDefaults() { $query->expects($this->once())->method('contain')->with([ 'ArticleTag' => [ 'conditions' => ['ArticleTag.article_id in' => $keys], - 'filtering' => true + 'matching' => true ] ]); @@ -377,7 +377,7 @@ public function testEagerLoaderWithOverrides() { $query->expects($this->once())->method('contain')->with([ 'ArticleTag' => [ 'conditions' => ['ArticleTag.article_id in' => $keys], - 'filtering' => true + 'matching' => true ] ]); @@ -501,7 +501,7 @@ public function testEagerLoaderSubquery() { $query->expects($this->once())->method('contain')->with([ 'ArticleTag' => [ 'conditions' => ['ArticleTag.article_id in' => $expected], - 'filtering' => true + 'matching' => true ] ]); diff --git a/lib/Cake/Test/TestCase/ORM/QueryTest.php b/lib/Cake/Test/TestCase/ORM/QueryTest.php index 72258325e1b..6e923d20354 100644 --- a/lib/Cake/Test/TestCase/ORM/QueryTest.php +++ b/lib/Cake/Test/TestCase/ORM/QueryTest.php @@ -666,4 +666,40 @@ public function testBelongsToManyEagerLoading($strategy) { $this->assertEquals($table->association('Tag')->strategy(), $strategy); } +/** + * Tests that tables results can be filtered by the result of a HasMany + * + * @return void + */ + public function testFilteringByHasMany() { + $this->_insertRecords(); + + $query = new Query($this->connection); + $table = Table::build('author', ['connection' => $this->connection]); + Table::build('article', ['connection' => $this->connection]); + $table->hasMany('article', ['property' => 'articles']); + + $results = $query->repository($table) + ->select() + ->contain(['article' => [ + 'matching' => true, + 'conditions' => ['Article.id' => 2] + ]]) + ->toArray(); + $expected = [ + [ + 'id' => 2, + 'name' => 'Bruce Lee', + 'articles' => [ + 'id' => 2, + 'title' => 'another title', + 'body' => 'another body', + 'author_id' => 2 + ] + ] + ]; + $this->assertEquals($expected, $results); + } + + }