Skip to content

Commit

Permalink
Postgres and SQL Server require an order clause for subqueries having…
Browse files Browse the repository at this point in the history
… a distinct
  • Loading branch information
lorenzo committed Jan 31, 2015
1 parent 3e8f150 commit 1748bb7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -240,7 +240,17 @@ protected function _buildSubquery($query)
}

$fields = $query->aliasFields($keys, $this->source()->alias());
return $filterQuery->select($fields, true)->distinct();
$filterQuery->select($fields, true)->distinct();

$order = $filterQuery->clause('order');
if ($order) {
$order->iterateParts(function ($dir, $field) use ($filterQuery) {
$filterQuery->select(is_int($field) ? $dir : $field);
return $dir;
});
}

return $filterQuery;
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -754,6 +754,7 @@ public function testFindMatchingWithContain()
->matching('Users', function ($q) {
return $q->where(['Users.id >=' => 1]);
})
->order(['Comments.id' => 'ASC'])
->first();
$this->assertInstanceOf('Cake\ORM\Entity', $result->article);
$this->assertInstanceOf('Cake\ORM\Entity', $result->user);
Expand Down

0 comments on commit 1748bb7

Please sign in to comment.