Skip to content

Commit

Permalink
Adding a fix for #5463
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 23, 2014
1 parent 16c36b1 commit f111af3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/ORM/ResultSet.php
Expand Up @@ -283,11 +283,12 @@ protected function _calculateAssociationMap() {
$map = $this->_query->eagerLoader()->associationsMap($this->_defaultTable);
$this->_matchingMap = (new Collection($map))
->match(['matching' => true])
->compile();
->toArray();

$this->_containMap = (new Collection(array_reverse($map)))
->match(['matching' => false])
->compile();
->indexBy('nestKey')
->toArray();
}

/**
Expand Down Expand Up @@ -331,7 +332,10 @@ protected function _groupResult($row) {
}
list($table, $field) = explode('__', $key);
$results['_matchingData'][$table][$field] = $value;
unset($row[$key]);

if (!isset($this->_containMap[$table])) {
unset($row[$key]);
}
}
if (empty($results['_matchingData'][$matching['alias']])) {
continue;
Expand All @@ -343,6 +347,7 @@ protected function _groupResult($row) {
);

if ($this->_hydrate) {
$options['source'] = $matching['alias'];
$entity = new $matching['entityClass']($results['_matchingData'][$matching['alias']], $options);
$entity->clean();
$results['_matchingData'][$matching['alias']] = $entity;
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -576,4 +576,23 @@ public function testFirstOnResultSet() {
$this->assertCount(3, $results->toArray());
}

/**
* Checks that matching and contain can be called for the same belongsTo association
*
* @see https://github.com/cakephp/cakephp/issues/5463
* @return void
*/
public function testFindMatchingAndContain() {
$table = TableRegistry::get('Articles');
$table->belongsTo('Authors');
$article = $table->find()
->contain('Authors')
->matching('Authors', function ($q) {
return $q->where(['Authors.id' => 1]);
})
->first();
$this->assertNotNull($article->author);
$this->assertEquals($article->author, $article->_matchingData['Authors']);
}

}

0 comments on commit f111af3

Please sign in to comment.