mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Fixed unlimitedRowCount() for grouped queries
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@103613 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
1df4ba601e
commit
6f9aa32ccc
@ -419,12 +419,6 @@ class SQLQuery {
|
||||
|
||||
/// VARIOUS TRANSFORMATIONS BELOW
|
||||
|
||||
/**
|
||||
* Return the number of rows in this query if the limit were removed. Useful in paged data sets.
|
||||
* @return int
|
||||
*
|
||||
* TODO Respect HAVING and GROUPBY, which can affect the result-count
|
||||
*/
|
||||
function unlimitedRowCount( $column = null) {
|
||||
// we can't clear the select if we're relying on its output by a HAVING clause
|
||||
if(count($this->having)) {
|
||||
@ -432,19 +426,26 @@ class SQLQuery {
|
||||
return $records->numRecords();
|
||||
}
|
||||
|
||||
$clone = clone $this;
|
||||
$clone->limit = null;
|
||||
$clone->orderby = null;
|
||||
|
||||
// Choose a default column
|
||||
if($column == null) {
|
||||
if($this->groupby) {
|
||||
$column = 'DISTINCT ' . implode(", ", $this->groupby);
|
||||
$countQuery = new SQLQuery();
|
||||
$countQuery->select = array("count(*)");
|
||||
$countQuery->from = array('(' . $clone->sql() . ') as all_distinct');
|
||||
|
||||
return $countQuery->execute()->value();
|
||||
|
||||
} else {
|
||||
$column = '*';
|
||||
$clone->select = array("count(*)");
|
||||
}
|
||||
} else {
|
||||
$clone->select = array("count($column)");
|
||||
}
|
||||
|
||||
$clone = clone $this;
|
||||
$clone->select = array("count($column)");
|
||||
$clone->limit = null;
|
||||
$clone->orderby = null;
|
||||
$clone->groupby = null;
|
||||
return $clone->execute()->value();
|
||||
}
|
||||
|
@ -741,6 +741,16 @@ class DataObjectTest extends SapphireTest {
|
||||
$this->assertEquals("Prop", $player->Position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the queries generated for many-many relation queries can have unlimitedRowCount
|
||||
* called on them.
|
||||
*/
|
||||
function testManyManyUnlimitedRowCount() {
|
||||
$player = $this->objFromFixture('DataObjectTest_Player', 'player2');
|
||||
$query = $player->getManyManyComponentsQuery('Teams');
|
||||
$this->assertEquals(2, $query->unlimitedRowCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that singular_name() generates sensible defaults.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user