diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index 4b836bfed..7599ffac7 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' + ); } }