Skip to content

Commit

Permalink
Remove offset from cloned queries used in subquery loads.
Browse files Browse the repository at this point in the history
Having an offset in a subquery causes SQL errors in MySQL. Also
include a test to ensure HAVING is not an issue.

Fixes #4465
  • Loading branch information
markstory committed Sep 3, 2014
1 parent 0368cbd commit 89a56a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -161,6 +161,7 @@ protected abstract function _linkField($options);
protected function _buildSubquery($query) {
$filterQuery = clone $query;
$filterQuery->limit(null);
$filterQuery->offset(null);
$filterQuery->order([], true);
$filterQuery->contain([], true);
$joins = $filterQuery->join();
Expand Down
21 changes: 20 additions & 1 deletion tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -39,7 +39,8 @@ class QueryRegressionTest extends TestCase {
'core.tag',
'core.articles_tag',
'core.author',
'core.special_tag'
'core.special_tag',
'core.translate',
];

/**
Expand Down Expand Up @@ -405,4 +406,22 @@ public function testAssociationChainOrder() {
$this->assertNotEmpty($resultA->articles_tag->author);
}

/**
* Test that offset/limit are elided from subquery loads.
*
* @return void
*/
public function testAssociationSubQueryNoOffset() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$table->locale('eng');
$query = $table->find('translations')->limit(10)->offset(1);
$result = $query->toArray();
$this->assertCount(2, $result);

$query = $table->find('translations')->having(['Articles.id >' => 1]);
$result = $query->toArray();
$this->assertCount(2, $result);
}

}

0 comments on commit 89a56a4

Please sign in to comment.