Merge pull request #4171 from dhensby/pulls/having-count

SQLQuery::Count errors with having clause
This commit is contained in:
Damian Mooyman 2015-05-08 09:37:48 +12:00
commit dca8e2ad52
2 changed files with 22 additions and 2 deletions

View File

@ -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 {

View File

@ -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());