mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUG SQLQuery::aggregate() with limit, groupBy and orderBy (fixes #8148)
This commit is contained in:
parent
8ec3641e60
commit
e53280c650
@ -1060,18 +1060,22 @@ class SQLQuery {
|
||||
|
||||
/**
|
||||
* Return a new SQLQuery that calls the given aggregate functions on this data.
|
||||
*
|
||||
* @param $column An aggregate expression, such as 'MAX("Balance")', or a set of them (as an escaped SQL statement)
|
||||
* @param $alias An optional alias for the aggregate column.
|
||||
*/
|
||||
public function aggregate($column) {
|
||||
if($this->groupby || $this->limit) {
|
||||
throw new Exception("SQLQuery::aggregate() doesn't work with groupby or limit, yet");
|
||||
}
|
||||
public function aggregate($column, $alias = null) {
|
||||
|
||||
$clone = clone $this;
|
||||
$clone->setLimit(array());
|
||||
$clone->setOrderBy(array());
|
||||
$clone->setGroupBy(array());
|
||||
$clone->setLimit($this->limit);
|
||||
$clone->setOrderBy($this->orderby);
|
||||
$clone->setGroupBy($this->groupby);
|
||||
if($alias) {
|
||||
$clone->selectField($column, $alias);
|
||||
} else {
|
||||
$clone->setSelect($column);
|
||||
}
|
||||
|
||||
|
||||
return $clone;
|
||||
}
|
||||
|
@ -368,6 +368,19 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests aggregate() function
|
||||
*/
|
||||
public function testAggregate() {
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setGroupBy("Common");
|
||||
|
||||
$queryClone = $query->aggregate('COUNT(*)', 'cnt');
|
||||
$result = $queryClone->execute();
|
||||
$this->assertEquals(array(2), $result->column('cnt'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that "_SortColumn0" is added for an aggregate in the ORDER BY
|
||||
* clause, in combination with a LIMIT and GROUP BY clause.
|
||||
@ -399,6 +412,7 @@ class SQLQueryTest_DO extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
"Name" => "Varchar",
|
||||
"Meta" => "Varchar",
|
||||
"Common" => "Varchar",
|
||||
"Date" => "SS_Datetime"
|
||||
);
|
||||
}
|
||||
|
@ -2,8 +2,10 @@ SQLQueryTest_DO:
|
||||
test1:
|
||||
Name: 'Object 1'
|
||||
Meta: 'Details 1'
|
||||
Common: 'Common Value'
|
||||
Date: 2012-01-01 10:00:00
|
||||
test2:
|
||||
Name: 'Object 2'
|
||||
Meta: 'Details 2'
|
||||
Date: 2012-05-01 09:00:00
|
||||
Common: 'Common Value'
|
Loading…
x
Reference in New Issue
Block a user