From e94c2a944e2212d3a82c299a3319fdbf775a78bb Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Thu, 7 May 2015 18:55:37 +0100 Subject: [PATCH 1/2] Test to prove having count issue --- tests/model/SQLQueryTest.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/model/SQLQueryTest.php b/tests/model/SQLQueryTest.php index 12cf229f7..42a4f42e0 100755 --- a/tests/model/SQLQueryTest.php +++ b/tests/model/SQLQueryTest.php @@ -7,7 +7,22 @@ class SQLQueryTest extends SapphireTest { protected $extraDataObjects = array( 'SQLQueryTest_DO', ); - + + public function testCount() { + + //basic counting + $qry = SQLQueryTest_DO::get()->dataQuery()->getFinalisedQuery(); + $qry->setGroupBy('Common'); + $ids = $this->allFixtureIDs('SQLQueryTest_DO'); + $this->assertEquals(count($ids), $qry->count('"SQLQueryTest_DO"."ID"')); + + //test with `having` + if (DB::getConn() instanceof MySQLDatabase) { + $qry->setHaving('"Date" > 2012-02-01'); + $this->assertEquals(1, $qry->count('"SQLQueryTest_DO"."ID"')); + } + } + public function testEmptyQueryReturnsNothing() { $query = new SQLQuery(); $this->assertEquals('', $query->sql()); From be10d90cc5936a1054c85709203789c537c3eeff Mon Sep 17 00:00:00 2001 From: Aram Balakjian Date: Thu, 30 Apr 2015 16:04:26 +0100 Subject: [PATCH 2/2] BUG count breaks when having clause defined --- model/SQLQuery.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/model/SQLQuery.php b/model/SQLQuery.php index c1c0e5b3f..85a2b241b 100644 --- a/model/SQLQuery.php +++ b/model/SQLQuery.php @@ -1038,8 +1038,13 @@ class SQLQuery { * @return int */ public function count( $column = null) { + // we can't clear the select if we're relying on its output by a HAVING clause + if(!empty($this->having)) { + $records = $this->execute(); + return $records->numRecords(); + } // Choose a default column - if($column == null) { + elseif($column == null) { if($this->groupby) { $column = 'DISTINCT ' . implode(", ", $this->groupby); } else {