From 472fc4ebb4a6459bb8485e0a60c14485b2ec56a3 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 31 May 2021 16:50:58 +1200 Subject: [PATCH] BUG Update DataQuery::exists to return false when limit causes no result to be returned (#9946) * BUG Update DataQuery::exists to return false when limit causes no result to be returned * Update comment * Fixing linting issue --- src/ORM/DataQuery.php | 3 +-- tests/php/ORM/DataQueryTest.php | 48 ++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index c15bb569d..fa817240b 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -465,10 +465,9 @@ class DataQuery // 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 diff --git a/tests/php/ORM/DataQueryTest.php b/tests/php/ORM/DataQueryTest.php index 1b9a3a1a4..9f7476376 100644 --- a/tests/php/ORM/DataQueryTest.php +++ b/tests/php/ORM/DataQueryTest.php @@ -466,8 +466,50 @@ class DataQueryTest extends SapphireTest 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( + 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' + ); } }