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

BUG Update DataQuery::exists to return false when limit causes no result to be returned #9946

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/ORM/DataQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,9 @@ public function exists(): bool
// statement anyway
$statement = $this->getFinalisedQuery();

// Clear limit, distinct, and order as it's not relevant for an exists query
// Clear distinct, and order as it's not relevant for an exists query
$statement->setDistinct(false);
$statement->setOrderBy(null);
$statement->setLimit(null);

// We can remove grouping if there's no "having" that might be relying on an aggregate
// Additionally, the columns being selected no longer matter
Expand Down
49 changes: 46 additions & 3 deletions tests/php/ORM/DataQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,51 @@ public function testMultipleRelationSort()

public function testExistsCreatesFunctionalQueries()
{
$this->assertTrue(ObjectE::get()->exists());
$this->assertFalse(ObjectE::get()->where(['"Title" = ?' => 'Foo'])->exists());
$this->assertTrue(ObjectE::get()->dataQuery()->groupby('"SortOrder"')->exists());
$this->assertTrue(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the scenarios here to test both the DataList exist and the DataQuery exists. I've added scenarios for the limit and added explicit message for each assertion.

ObjectE::get()->exists(),
'Query for ObjectE exists because there\'s more than 1 record'
);
$this->assertFalse(
ObjectE::get()->where(['"Title" = ?' => 'Foo'])->exists(),
'Query for ObjectE with Title Foo does NOT exists because there\'s no matching record'
);
$this->assertTrue(
ObjectE::get()->dataQuery()->groupby('"SortOrder"')->exists(),
'Existence of query for ObjectE is not affected by group by'
);
$this->assertTrue(
ObjectE::get()->limit(1)->exists(),
'Existence of query for ObjectE is not affected by limit if records are returned'
);
$this->assertFalse(
ObjectE::get()->limit(4, 9999)->exists(),
'Existence of query for ObjectE is affected by limit if no records are returned'
);

$query = new DataQuery(ObjectE::class);
$this->assertTrue(
$query->exists(),
'exist returns true if query return results'
);
$query = new DataQuery(ObjectE::class);
$this->assertFalse(
$query->where(['"Title" = ?' => 'Foo'])->exists(),
'exist returns false if there\'s no results'
);
$query = new DataQuery(ObjectE::class);
$this->assertTrue(
$query->groupby('"SortOrder"')->exists(),
'exist is unaffected by group by'
);
$query = new DataQuery(ObjectE::class);
$this->assertTrue(
$query->limit(1)->exists(),
'exist is unaffected by limit as long as one recard is returned'
);
$this->assertFalse(
$query->limit(1, 9999)->exists(),
'exist is false when a limit returns no results'
);
}

maxime-rainville marked this conversation as resolved.
Show resolved Hide resolved
}