From ce4067608ce7150d9348e46173a9ba365acd971d Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 4 Feb 2010 00:52:43 +0000 Subject: [PATCH] BUGFIX: Make TableListField rely on SQLQuery for its count-generation. BUGFIX: Make SQLQuery return an appropriate count if a HAVING clause is used. (from r95814) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@98094 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/SQLQuery.php | 6 ++++++ forms/TableListField.php | 17 +---------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/core/model/SQLQuery.php b/core/model/SQLQuery.php index 06e8d59b7..3e109448c 100755 --- a/core/model/SQLQuery.php +++ b/core/model/SQLQuery.php @@ -426,6 +426,12 @@ class SQLQuery { * 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)) { + $records = $this->execute(); + return $records->numRecords(); + } + // Choose a default column if($column == null) { if($this->groupby) { diff --git a/forms/TableListField.php b/forms/TableListField.php index e96f01b0a..6f0e053c8 100755 --- a/forms/TableListField.php +++ b/forms/TableListField.php @@ -852,23 +852,8 @@ JS if($this->customSourceItems) { return $this->customSourceItems->Count(); } - $countQuery = $this->getQuery(); - $countQuery->orderby = array(); - $baseClass = ClassInfo::baseDataClass($this->sourceClass); - // we can't clear the select if we're relying on its output by a HAVING clause - if(count($countQuery->having) || count($countQuery->groupby)) { - $records = $countQuery->execute(); - // TODO figure out how to use COUNT and GROUBY together to produce a single rowcount - $this->totalCount = $records->numRecords(); - } else { - $countQuery->select = array(); - $countQuery->groupby = array(); - $countQuery->select[] = "COUNT(DISTINCT \"{$baseClass}\".\"ID\") AS \"TotalCount\""; - $records = $countQuery->execute(); - $record = $records->nextRecord(); - $this->totalCount = $record['TotalCount']; - } + $this->totalCount = $this->getQuery()->unlimitedRowCount(); return $this->totalCount; }