Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing eager loading when using the subquery strategy, it was matching

more rows than it should have
  • Loading branch information...
commit 5b733cc36d673f5923416472e7abc3cb678da287 1 parent 625c562
@lorenzo lorenzo authored
View
3  src/ORM/Association/ExternalAssociationTrait.php
@@ -320,7 +320,8 @@ protected function _buildSubquery($query, $foreignKey) {
}
}
$filterQuery->join($joins, [], true);
- return $filterQuery->select($foreignKey, true);
+ $fields = $query->aliasFields((array)$query->repository()->primaryKey());
+ return $filterQuery->select($fields, true);
}
/**
View
12 tests/TestCase/ORM/Association/BelongsToManyTest.php
@@ -669,11 +669,14 @@ public function testEagerLoaderSubquery() {
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
- 'tag_id' => ['type' => 'integer']
+ 'tag_id' => ['type' => 'integer'],
+ '_constraints' => [
+ 'PK' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']]
+ ]
]
]);
$association = new BelongsToMany('Tags', $config);
- $parent = (new Query(null, null))
+ $parent = (new Query(null, $this->article))
->join(['foo' => ['table' => 'foo', 'type' => 'inner', 'conditions' => []]])
->join(['bar' => ['table' => 'bar', 'type' => 'left', 'conditions' => []]]);
$parent->hydrate(false);
@@ -681,8 +684,9 @@ public function testEagerLoaderSubquery() {
$query = $this->getMock(
'Cake\ORM\Query',
['all', 'where', 'andWhere', 'order', 'select', 'matching'],
- [null, null]
+ [null, $this->article]
);
+
$query->hydrate(false);
$this->tag->expects($this->once())
@@ -702,7 +706,7 @@ public function testEagerLoaderSubquery() {
unset($joins[1]);
$expected
->contain([], true)
- ->select('ArticlesTags.article_id', true)
+ ->select(['Articles__id' => 'Articles.id'], true)
->join($joins, [], true);
$query->expects($this->at(0))->method('where')
View
4 tests/TestCase/ORM/Association/HasManyTest.php
@@ -307,7 +307,7 @@ public function testEagerLoaderSubquery() {
'targetTable' => $this->article,
];
$association = new HasMany('Articles', $config);
- $parent = (new Query(null, null))
+ $parent = (new Query(null, $this->author))
->join(['foo' => ['table' => 'foo', 'type' => 'inner', 'conditions' => []]])
->join(['bar' => ['table' => 'bar', 'type' => 'left', 'conditions' => []]]);
@@ -338,7 +338,7 @@ public function testEagerLoaderSubquery() {
unset($joins[1]);
$expected
->contain([], true)
- ->select('Articles.author_id', true)
+ ->select(['Authors__id' => 'Authors.id'], true)
->join($joins, [], true);
$query->expects($this->once())->method('andWhere')
->with(['Articles.author_id IN' => $expected])
Please sign in to comment.
Something went wrong with that request. Please try again.