mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-28 12:26:23 +02:00
Merge pull request #4171 from dhensby/pulls/having-count
SQLQuery::Count errors with having clause
This commit is contained in:
commit
dca8e2ad52
@ -1038,8 +1038,13 @@ class SQLQuery {
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function count( $column = null) {
|
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
|
// Choose a default column
|
||||||
if($column == null) {
|
elseif($column == null) {
|
||||||
if($this->groupby) {
|
if($this->groupby) {
|
||||||
$column = 'DISTINCT ' . implode(", ", $this->groupby);
|
$column = 'DISTINCT ' . implode(", ", $this->groupby);
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,6 +8,21 @@ class SQLQueryTest extends SapphireTest {
|
|||||||
'SQLQueryTest_DO',
|
'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() {
|
public function testEmptyQueryReturnsNothing() {
|
||||||
$query = new SQLQuery();
|
$query = new SQLQuery();
|
||||||
$this->assertEquals('', $query->sql());
|
$this->assertEquals('', $query->sql());
|
||||||
|
Loading…
Reference in New Issue
Block a user