diff --git a/src/Database/Query.php b/src/Database/Query.php index 51884f40995..989ef62285a 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -659,6 +659,7 @@ public function join($tables = null, $types = [], $overwrite = false) { $types += $this->defaultTypes(); $joins = []; + $i = count($this->_parts['join']); foreach ($tables as $alias => $t) { if (!is_array($t)) { $t = ['table' => $t, 'conditions' => $this->newExpr()]; @@ -666,13 +667,14 @@ public function join($tables = null, $types = [], $overwrite = false) { if (!($t['conditions']) instanceof ExpressionInterface) { $t['conditions'] = $this->newExpr()->add($t['conditions'], $types); } - $joins[] = $t + ['type' => 'INNER', 'alias' => is_string($alias) ? $alias : null]; + $alias = is_string($alias) ? $alias : null; + $joins[$alias ?: $i++] = $t + ['type' => 'INNER', 'alias' => $alias]; } if ($overwrite) { $this->_parts['join'] = $joins; } else { - $this->_parts['join'] = array_merge($this->_parts['join'], array_values($joins)); + $this->_parts['join'] = array_merge($this->_parts['join'], $joins); } $this->_dirty(); diff --git a/tests/TestCase/ORM/Association/BelongsToManyTest.php b/tests/TestCase/ORM/Association/BelongsToManyTest.php index a3fc50d315b..1a586c3524d 100644 --- a/tests/TestCase/ORM/Association/BelongsToManyTest.php +++ b/tests/TestCase/ORM/Association/BelongsToManyTest.php @@ -703,7 +703,7 @@ public function testEagerLoaderSubquery() { $expected = clone $parent; $joins = $expected->join(); - unset($joins[1]); + unset($joins['bar']); $expected ->contain([], true) ->select(['Articles__id' => 'Articles.id'], true) diff --git a/tests/TestCase/ORM/Association/HasManyTest.php b/tests/TestCase/ORM/Association/HasManyTest.php index e6ec7b4ab82..af5c9025d9d 100644 --- a/tests/TestCase/ORM/Association/HasManyTest.php +++ b/tests/TestCase/ORM/Association/HasManyTest.php @@ -335,7 +335,7 @@ public function testEagerLoaderSubquery() { $expected = clone $parent; $joins = $expected->join(); - unset($joins[1]); + unset($joins['bar']); $expected ->contain([], true) ->select(['Authors__id' => 'Authors.id'], true) diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index bde1fcccf33..717dc673b85 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -999,7 +999,7 @@ public function testApplyOptions() { $expected = new QueryExpression(['a > b']); $result = $query->clause('join'); $this->assertEquals([ - ['alias' => 'table_a', 'type' => 'INNER', 'conditions' => $expected] + 'table_a' => ['alias' => 'table_a', 'type' => 'INNER', 'conditions' => $expected] ], $result); $expected = new OrderByExpression(['a' => 'ASC']); @@ -1818,4 +1818,23 @@ public function testQueryWithStackedFormatters() { $this->assertEquals($expected, $query->toArray()); } +/** + * Tests that getting results from a query having a contained association + * will no attach joins twice if count() is called on it afterwards + * + * @return void + */ + public function testCountWithContainCallingAll() { + $table = TableRegistry::get('articles'); + $table->belongsTo('authors'); + $query = $table->find() + ->select(['id', 'title']) + ->contain('authors') + ->limit(2); + + $results = $query->all(); + $this->assertCount(2, $results); + $this->assertEquals(3, $query->count()); + } + }