Skip to content

Commit 9969273

Browse files
committed
Making sure contain and matching clashes are always resolved, fixes #5584
1 parent 63a7531 commit 9969273

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/ORM/EagerLoader.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,15 @@ protected function _resolveJoins($associations, $matching = [])
465465
$options->associations(),
466466
$inMatching ? $mathching[$table] : []
467467
);
468-
} else {
469-
$options->canBeJoined(false);
470-
$this->_loadExternal[] = $options;
468+
continue;
469+
}
470+
471+
if ($inMatching) {
472+
$this->_correctStrategy($options, $table);
471473
}
474+
475+
$options->canBeJoined(false);
476+
$this->_loadExternal[] = $options;
472477
}
473478
return $result;
474479
}

tests/TestCase/ORM/QueryRegressionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,4 +670,30 @@ public function testFindMatchingOverwrite()
670670
$this->assertNotNull($result->article);
671671
$this->assertEquals($result->article, $result->_matchingData['Articles']);
672672
}
673+
674+
/**
675+
* Tests that matching does not overwrite associations in contain
676+
*
677+
* @see https://github.com/cakephp/cakephp/issues/5584
678+
* @return void
679+
*/
680+
public function testFindMatchingOverwrite2()
681+
{
682+
$comments = TableRegistry::get('Comments');
683+
$comments->belongsTo('Articles');
684+
685+
$articles = TableRegistry::get('Articles');
686+
$articles->belongsTo('Authors');
687+
$articles->belongsToMany('Tags');
688+
689+
$result = $comments
690+
->find()
691+
->matching('Articles.Tags', function($q) {
692+
return $q->where(['Tags.id' => 2]);
693+
})
694+
->contain('Articles.Authors')
695+
->first();
696+
697+
$this->assertNotNull($result->article->author);
698+
}
673699
}

0 commit comments

Comments
 (0)