diff --git a/model/DataQuery.php b/model/DataQuery.php index 4e4178903..93e2e7461 100644 --- a/model/DataQuery.php +++ b/model/DataQuery.php @@ -240,16 +240,16 @@ class DataQuery { // don't touch functions in the ORDER BY or function calls // selected as fields if(strpos($k, '(') !== false) continue; - - // Pull through SortColumn references from the originalSelect variables - if(preg_match('/_SortColumn/', $k)) { - if(isset($originalSelect[$k])) $query->selectField($originalSelect[$k], $k); - continue; - } - + $col = str_replace('"', '', trim($k)); $parts = explode('.', $col); + // Pull through SortColumn references from the originalSelect variables + if(preg_match('/_SortColumn/', $col)) { + if(isset($originalSelect[$col])) $query->selectField($originalSelect[$col], $col); + continue; + } + if(count($parts) == 1) { $databaseFields = DataObject::database_fields($baseClass); diff --git a/model/SQLQuery.php b/model/SQLQuery.php index 0bc9a850a..2e49cdb99 100644 --- a/model/SQLQuery.php +++ b/model/SQLQuery.php @@ -383,9 +383,9 @@ class SQLQuery { $clause = trim($clause); $column = "_SortColumn{$i}"; - + $this->selectField($clause, $column); - $this->orderby($column, $dir, false); + $this->orderby('"' . $column . '"', $dir, false); $i++; } }