Skip to content

Commit 82a7e55

Browse files
committed
Add addOrderEscape / addOrderEscapes for raw order_by support
1 parent f98cf2d commit 82a7e55

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ $datatables->addColumnAliases([
176176
$datatables->addSequenceNumber();
177177
$datatables->addSequenceNumber('rowNumber'); // It will be rowNumber
178178

179+
// Add order escape for a specific column
180+
// Useful when ordering by raw expressions or aggregate functions like COUNT()
181+
// This will skip identifier protection for the given column
182+
$datatables->addOrderEscape('name', FALSE);
183+
184+
// Add order escapes for multiple columns at once
185+
// Each key is the column name, and the value is the escape flag (TRUE/FALSE)
186+
$datatables->addOrderEscapes([
187+
'name' => FALSE,
188+
'category' => FALSE
189+
]);
190+
179191
// Don't forget to call generate to get the results
180192
$datatables->generate();
181193
```

src/DataTables.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)