Skip to content

Commit 158eec4

Browse files
authored
Merge pull request #601 from thingNumber1/patch-1
Adding ORDER BY REGEXP possibilites
2 parents 72d231d + dd37055 commit 158eec4

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

MysqliDb.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,16 +1080,16 @@ public function loadXml($importTable, $importFile, $importSettings = null)
10801080
/**
10811081
* This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries.
10821082
*
1083-
* @uses $MySqliDb->orderBy('id', 'desc')->orderBy('name', 'desc');
1083+
* @uses $MySqliDb->orderBy('id', 'desc')->orderBy('name', 'desc', '^[a-z]')->orderBy('name', 'desc');
10841084
*
10851085
* @param string $orderByField The name of the database field.
10861086
* @param string $orderByDirection Order direction.
1087-
* @param array $customFields Fieldset for ORDER BY FIELD() ordering
1087+
* @param mixed $customFieldsOrRegExp Array with fieldset for ORDER BY FIELD() ordering or string with regular expresion for ORDER BY REGEXP ordering
10881088
*
10891089
* @throws Exception
10901090
* @return MysqliDb
10911091
*/
1092-
public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields = null)
1092+
public function orderBy($orderByField, $orderbyDirection = "DESC", $customFieldsOrRegExp = null)
10931093
{
10941094
$allowedDirection = Array("ASC", "DESC");
10951095
$orderbyDirection = strtoupper(trim($orderbyDirection));
@@ -1105,13 +1105,16 @@ public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields
11051105
throw new Exception('Wrong order direction: ' . $orderbyDirection);
11061106
}
11071107

1108-
if (is_array($customFields)) {
1109-
foreach ($customFields as $key => $value) {
1110-
$customFields[$key] = preg_replace("/[^-a-z0-9\.\(\),_` ]+/i", '', $value);
1108+
if (is_array($customFieldsOrRegExp)) {
1109+
foreach ($customFieldsOrRegExp as $key => $value) {
1110+
$customFieldsOrRegExp[$key] = preg_replace("/[^-a-z0-9\.\(\),_` ]+/i", '', $value);
11111111
}
1112-
1113-
$orderByField = 'FIELD (' . $orderByField . ', "' . implode('","', $customFields) . '")';
1114-
}
1112+
$orderByField = 'FIELD (' . $orderByField . ', "' . implode('","', $customFieldsOrRegExp) . '")';
1113+
}elseif(is_string($customFieldsOrRegExp)){
1114+
$orderByField = $orderByField . " REGEXP '" . $customFieldsOrRegExp . "'";
1115+
}elseif($customFieldsOrRegExp !== null){
1116+
throw new Exception('Wrong custom field or Regular Expression: ' . $customFieldsOrRegExp);
1117+
}
11151118

11161119
$this->_orderBy[$orderByField] = $orderbyDirection;
11171120
return $this;

0 commit comments

Comments
 (0)