@@ -17,6 +17,7 @@ class DataTables
1717 protected $ returnedFieldNames = [];
1818 protected $ formatters = [];
1919 protected $ extraColumns = [];
20+ protected $ orderEscapes = [];
2021
2122 protected $ recordsTotal ;
2223 protected $ recordsFiltered ;
@@ -110,6 +111,41 @@ public function addColumnAliases($aliases)
110111 return $ this ;
111112 }
112113
114+ /**
115+ * Add an order escape rule for a specific column
116+ * Useful when ordering by raw expressions or columns with aggregate functions
117+ * @param string $column The column name
118+ * @param bool $escape Whether to escape/protect the identifier (TRUE/FALSE)
119+ *
120+ * @return $this
121+ */
122+ public function addOrderEscape ($ column , $ escape )
123+ {
124+ $ column = $ this ->resolveColumnName ($ column );
125+
126+ $ this ->orderEscapes [$ column ] = $ escape ;
127+ return $ this ;
128+ }
129+
130+ /**
131+ * Add multiple order escape rules at once
132+ * @param array $columns An array of column => escape_boolean pairs
133+ *
134+ * @return $this
135+ */
136+ public function addOrderEscapes ($ columns )
137+ {
138+ if (!is_array ($ columns )) {
139+ throw new \Exception ('The $columns parameter must be an array with key => escape_boolean. ' );
140+ }
141+
142+ foreach ($ columns as $ column => $ escape ) {
143+ $ this ->addOrderEscape ($ column , $ escape );
144+ }
145+
146+ return $ this ;
147+ }
148+
113149 /**
114150 * Only return the column as defined
115151 * @param string|array $columns The columns to only will be returned
@@ -273,8 +309,12 @@ protected function order()
273309
274310 if (empty ($ column )) continue ;
275311
312+ $ escape = isset ($ this ->orderEscapes [$ column ])
313+ ? $ this ->orderEscapes [$ column ]
314+ : NULL ;
315+
276316 // Apply order
277- $ this ->queryBuilder ->{$ this ->config ->get ('orderBy ' )}($ column , $ order ['dir ' ]);
317+ $ this ->queryBuilder ->{$ this ->config ->get ('orderBy ' )}($ column , $ order ['dir ' ], $ escape );
278318 }
279319 }
280320 }
0 commit comments