mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00: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
|
/// 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) {
|
function unlimitedRowCount( $column = null) {
|
||||||
// we can't clear the select if we're relying on its output by a HAVING clause
|
// we can't clear the select if we're relying on its output by a HAVING clause
|
||||||
if(count($this->having)) {
|
if(count($this->having)) {
|
||||||
@ -432,19 +426,26 @@ class SQLQuery {
|
|||||||
return $records->numRecords();
|
return $records->numRecords();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$clone = clone $this;
|
||||||
|
$clone->limit = null;
|
||||||
|
$clone->orderby = null;
|
||||||
|
|
||||||
// Choose a default column
|
// Choose a default column
|
||||||
if($column == null) {
|
if($column == null) {
|
||||||
if($this->groupby) {
|
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 {
|
} 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;
|
$clone->groupby = null;
|
||||||
return $clone->execute()->value();
|
return $clone->execute()->value();
|
||||||
}
|
}
|
||||||
|
@ -741,6 +741,16 @@ class DataObjectTest extends SapphireTest {
|
|||||||
$this->assertEquals("Prop", $player->Position);
|
$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.
|
* Tests that singular_name() generates sensible defaults.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user