Skip to content

Commit

Permalink
Fixing issue in contain/mathching regarding dot notation
Browse files Browse the repository at this point in the history
Previously it was impossible to use dot notation on two
different calls to matching/contain when part of the string was
shared (for example Articles.SpecialTags.Tags and Articles.SpecialTags.Authors)

The issue was that the second call would override the settings
created for the first one.
  • Loading branch information
lorenzo committed Jun 16, 2015
1 parent c29c9e0 commit 60db9e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -281,7 +281,10 @@ protected function _reformatContain($associations, $original)
$options = isset($options['config']) ?
$options['config'] + $options['associations'] :
$options;
$options = $this->_reformatContain($options, []);
$options = $this->_reformatContain(
$options,
isset($pointer[$table]) ? $pointer[$table] : []
);
}

if ($options instanceof Closure) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/SpecialTagsFixture.php
Expand Up @@ -47,7 +47,7 @@ class SpecialTagsFixture extends TestFixture
* @var array
*/
public $records = [
['article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'author_id' => null],
['article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => null]
['article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'author_id' => 1],
['article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => 2]
];
}
29 changes: 29 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -968,4 +968,33 @@ public function testBooleanConditionsInContain()
$this->assertNotEmpty($result->tags, 'Missing tags');
$this->assertNotEmpty($result->tags[0]->_joinData, 'Missing join data');
}

/**
* Tests that it is possible to use matching with dot notation
* even when part of the part of the path in the dot notation is
* shared for two different calls
*
* @return void
*/
public function testDotNotationNotOverride()
{
$table = TableRegistry::get('Comments');
$articles = $table->belongsTo('Articles');
$specialTags = $articles->hasMany('SpecialTags');
$specialTags->belongsTo('Authors');
$specialTags->belongsTo('Tags');

$results = $table
->find()
->select(['name' => 'Authors.name', 'tag' => 'Tags.name'])
->matching('Articles.SpecialTags.Tags')
->matching('Articles.SpecialTags.Authors', function ($q) {
return $q->where(['Authors.id' => 2]);
})
->distinct()
->hydrate(false)
->toArray();

$this->assertEquals([['name' => 'nate', 'tag' => 'tag1']], $results);
}
}

0 comments on commit 60db9e0

Please sign in to comment.