Skip to content

Commit

Permalink
Ignore limit if no order exists when building subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Oct 6, 2021
1 parent 5b4b77b commit 8881d96
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ORM/Association/Loader/SelectLoader.php
Expand Up @@ -404,7 +404,8 @@ protected function _buildSubquery(Query $query): Query
$filterQuery->contain([], true);
$filterQuery->setValueBinder(new ValueBinder());

if (!$filterQuery->clause('limit')) {
// Ignore limit if there is no order since we need all rows to find matches
if (!$filterQuery->clause('limit') || !$filterQuery->clause('order')) {
$filterQuery->limit(null);
$filterQuery->order([], true);
$filterQuery->offset(null);
Expand Down
45 changes: 45 additions & 0 deletions tests/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -680,6 +680,51 @@ public function testValueBinderUpdateOnSubQueryStrategy()
$this->assertCount(2, $authorsAndArticles->get('articles'));
}

/**
* Tests using subquery strategy when parent query
* that contains limit without order.
*/
public function testSubqueryWithLimit()
{
$Authors = $this->getTableLocator()->get('Authors');
$Authors->hasMany('Articles', [
'strategy' => Association::STRATEGY_SUBQUERY,
]);

$query = $Authors->find();
$result = $query
->contain('Articles')
->first();

if (in_array($result->name, ['mariano', 'larry'])) {
$this->assertNotEmpty($result->articles);
} else {
$this->assertEmpty($result->articles);
}
}

/**
* Tests using subquery strategy when parent query
* that contains limit with order.
*/
public function testSubqueryWithLimitAndOrder()
{
$Authors = $this->getTableLocator()->get('Authors');
$Authors->hasMany('Articles', [
'strategy' => Association::STRATEGY_SUBQUERY,
]);

$query = $Authors->find();
$result = $query
->contain('Articles')
->order(['name' => 'ASC'])
->limit(2)
->toArray();

$this->assertCount(0, $result[0]->articles);
$this->assertCount(1, $result[1]->articles);
}

/**
* Assertion method for order by clause contents.
*
Expand Down

0 comments on commit 8881d96

Please sign in to comment.