Skip to content

Commit

Permalink
Refactoring to allow dot notation and testing matching method
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 5, 2014
1 parent 714ae00 commit 3f47f6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
14 changes: 12 additions & 2 deletions Cake/ORM/Query.php
Expand Up @@ -348,8 +348,18 @@ public function contain($associations = null, $override = false) {
* @return Query
*/
public function matching($assoc, callable $builder = null) {
$options = ['queryBuilder' => $builder, 'matching' => true];
return $this->contain([$assoc => array_filter($options)]);
$assocs = explode('.', $assoc);
$last = array_pop($assocs);
$containments = [];
$pointer =& $containments;

foreach ($assocs as $name) {
$pointer[$name] = ['matching' => true];
$pointer =& $pointer[$name];
}

$pointer[$last] = ['queryBuilder' => $builder, 'matching' => true];
return $this->contain($containments);
}

/**
Expand Down
21 changes: 9 additions & 12 deletions Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -837,10 +837,9 @@ public function testFilteringByHasManyNoHydration() {
$results = $query->repository($table)
->select()
->hydrate(false)
->contain(['articles' => [
'matching' => true,
'conditions' => ['articles.id' => 2]
]])
->matching('articles', function($q) {
return $q->where(['articles.id' => 2]);
})
->toArray();
$expected = [
[
Expand Down Expand Up @@ -875,10 +874,9 @@ public function testFilteringByBelongsToManyNoHydration() {
$table->belongsToMany('Tags');

$results = $query->repository($table)->select()
->contain(['Tags' => [
'matching' => true,
'conditions' => ['Tags.id' => 3]
]])
->matching('Tags', function($q) {
return $q->where(['Tags.id' => 3]);
})
->hydrate(false)
->toArray();
$expected = [
Expand All @@ -898,10 +896,9 @@ public function testFilteringByBelongsToManyNoHydration() {

$query = new Query($this->connection, $table);
$results = $query->select()
->contain(['Tags' => [
'matching' => true,
'conditions' => ['Tags.name' => 'tag2']]
])
->matching('Tags', function($q) {
return $q->where(['Tags.name' => 'tag2']);
})
->hydrate(false)
->toArray();
$expected = [
Expand Down

0 comments on commit 3f47f6d

Please sign in to comment.