Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore limit if no order exists when building subquery #16008

Merged
merged 1 commit into from Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends on the db since there is no order. Hard to force a result in the middle of these unit tests.

}
}

/**
* 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