From 7779e1c2b0a914ede0c58888ba042417a9d1ad65 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 22 Feb 2008 00:45:50 +0000 Subject: [PATCH] Fixed MySQL 4.1 support for situations where we are sorting by a group aggregation function Fixed notice level error git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@49992 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 30981399d..fc1c82a2a 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -862,7 +862,7 @@ class DataObject extends ViewableData implements DataObjectInterface { * @todo Implement query-params */ public function getManyManyComponents($componentName, $filter = "", $sort = "", $join = "", $limit = "") { - $sum = md5("{$filter}_{$sort}_{$join}_{$limit}_{$having}"); + $sum = md5("{$filter}_{$sort}_{$join}_{$limit}"); if(isset($this->componentCache[$componentName . '_' . $sum]) && false != $this->componentCache[$componentName . '_' . $sum]) { return $this->componentCache[$componentName . '_' . $sum]; } @@ -1380,9 +1380,32 @@ class DataObject extends ViewableData implements DataObjectInterface { } $baseClass = array_shift($tableClasses); + $select = array("`$baseClass`.*"); + + // If sort contains a function call, let's move the sort clause into a separate selected field. + // Some versions of MySQL choke if you have a group function referenced directly in the ORDER BY + if($sort && strpos($sort,'(') !== false) { + // Sort can be "Col1 DESC|ASC, Col2 DESC|ASC", we need to handle that + $sortParts = explode(",", $sort); + foreach($sortParts as $i => $sortPart) { + $sortPart = trim($sortPart); + if(substr(strtolower($sortPart),-5) == ' desc') { + $select[] = substr($sortPart,0,-5) . " AS _SortColumn{$i}"; + $newSorts[] = "_SortColumn{$i} DESC"; + } else if(substr(strtolower($sortPart),-4) == ' asc') { + $select[] = substr($sortPart,0,-4) . " AS _SortColumn{$i}"; + $newSorts[] = "_SortColumn{$i} ASC"; + } else { + $select[] = "$sortPart AS _SortColumn{$i}"; + $newSorts[] = "_SortColumn{$i} ASC"; + } + } + + $sort = implode(", ", $newSorts); + } // Build our intial query - $query = new SQLQuery(array("`$baseClass`.*"), "`$baseClass`", $filter, $sort); + $query = new SQLQuery($select, "`$baseClass`", $filter, $sort); // Join all the tables if($tableClasses) {