From 9969273cfc68fe5ae8698ea05cb85851a251d803 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Wed, 14 Jan 2015 20:45:03 +0100 Subject: [PATCH] Making sure contain and matching clashes are always resolved, fixes #5584 --- src/ORM/EagerLoader.php | 11 ++++++--- tests/TestCase/ORM/QueryRegressionTest.php | 26 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/ORM/EagerLoader.php b/src/ORM/EagerLoader.php index 26e6628983e..36d2cfacc3b 100644 --- a/src/ORM/EagerLoader.php +++ b/src/ORM/EagerLoader.php @@ -465,10 +465,15 @@ protected function _resolveJoins($associations, $matching = []) $options->associations(), $inMatching ? $mathching[$table] : [] ); - } else { - $options->canBeJoined(false); - $this->_loadExternal[] = $options; + continue; + } + + if ($inMatching) { + $this->_correctStrategy($options, $table); } + + $options->canBeJoined(false); + $this->_loadExternal[] = $options; } return $result; } diff --git a/tests/TestCase/ORM/QueryRegressionTest.php b/tests/TestCase/ORM/QueryRegressionTest.php index 4a44b2943d6..b66fda0ddb8 100644 --- a/tests/TestCase/ORM/QueryRegressionTest.php +++ b/tests/TestCase/ORM/QueryRegressionTest.php @@ -670,4 +670,30 @@ public function testFindMatchingOverwrite() $this->assertNotNull($result->article); $this->assertEquals($result->article, $result->_matchingData['Articles']); } + + /** + * Tests that matching does not overwrite associations in contain + * + * @see https://github.com/cakephp/cakephp/issues/5584 + * @return void + */ + public function testFindMatchingOverwrite2() + { + $comments = TableRegistry::get('Comments'); + $comments->belongsTo('Articles'); + + $articles = TableRegistry::get('Articles'); + $articles->belongsTo('Authors'); + $articles->belongsToMany('Tags'); + + $result = $comments + ->find() + ->matching('Articles.Tags', function($q) { + return $q->where(['Tags.id' => 2]); + }) + ->contain('Articles.Authors') + ->first(); + + $this->assertNotNull($result->article->author); + } }