From ecec061408c6c74fa8dfa786d00509ed26a02425 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 11 Mar 2008 01:29:14 +0000 Subject: [PATCH] Merged revisions 50179 via svnmerge from http://svn.silverstripe.com/open/modules/sapphire/branches/2.2.2 ........ r50179 | sminnee | 2008-02-26 12:03:10 +1300 (Tue, 26 Feb 2008) | 2 lines Fixed 4.1-sort-by-group-aggregate query rewriter for sort functions containing columns, eg, ORDER BY if(A,B,C), X ........ git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@50862 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObject.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/core/model/DataObject.php b/core/model/DataObject.php index fc1c82a2a..46f12b75f 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -1387,7 +1387,19 @@ class DataObject extends ViewableData implements DataObjectInterface { 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) { + + // If you have select if(X,A,B),C then the array will return 'if(X','A','B)','C'. + // Turn this into 'if(X,A,B)','C' by counting brackets + while(list($i,$sortPart) = each($sortParts)) { + while(substr_count($sortPart,'(') > substr_count($sortPart,')')) { + list($i,$nextSortPart) = each($sortParts); + if($i === null) break; + $sortPart .= ',' . $nextSortPart; + } + $lumpedSortParts[] = $sortPart; + } + + foreach($lumpedSortParts as $i => $sortPart) { $sortPart = trim($sortPart); if(substr(strtolower($sortPart),-5) == ' desc') { $select[] = substr($sortPart,0,-5) . " AS _SortColumn{$i}";