Skip to content

Commit

Permalink
Renaming filtering -> matching as it better expresses the purpose.
Browse files Browse the repository at this point in the history
Adding a unit test for it
  • Loading branch information
lorenzo committed May 30, 2013
1 parent 4e24975 commit 852b709
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -215,7 +215,7 @@ protected function _buildQuery($options) {

$options['contain'][$pivotAlias] = [
'conditions' => [$key . ' in' => $filter],
'filtering' => true
'matching' => true
];

if (!empty($options['fields'])) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -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']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/ORM/Query.php
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -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
]
]);

Expand Down Expand Up @@ -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
]
]);

Expand Down Expand Up @@ -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
]
]);

Expand Down Expand Up @@ -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
]
]);

Expand Down
36 changes: 36 additions & 0 deletions lib/Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -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);
}


}

0 comments on commit 852b709

Please sign in to comment.