Commit e5937d76 authored by Alexander Butenko's avatar Alexander Butenko

Order by field support

parent 06f6a372
......@@ -446,7 +446,7 @@ class MysqliDb
*
* @return MysqliDb
*/
public function orderBy($orderByField, $orderbyDirection = "DESC")
public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields = null)
{
$allowedDirection = Array ("ASC", "DESC");
$orderbyDirection = strtoupper (trim ($orderbyDirection));
......@@ -455,6 +455,13 @@ class MysqliDb
if (empty($orderbyDirection) || !in_array ($orderbyDirection, $allowedDirection))
die ('Wrong order direction: '.$orderbyDirection);
if (is_array ($customFields)) {
foreach ($customFields as $key => $value)
$customFields[$key] = preg_replace ("/[^-a-z0-9\.\(\),_]+/i",'', $value);
$orderByField = 'FIELD (' . $orderByField . ', "' . implode('","', $customFields) . '")';
}
$this->_orderBy[$orderByField] = $orderbyDirection;
return $this;
}
......
......@@ -266,6 +266,13 @@ $results = $db->get('users');
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
```
order by values example:
```php
$db->orderBy('userGroup', 'ASC', array('superuser', 'admin', 'users'));
$db->get('users');
// Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC;
```
### Grouping method
```php
$db->groupBy ("name");
......
......@@ -149,6 +149,14 @@ if ($db->count != 3) {
exit;
}
// order by field
$db->orderBy("login","asc", Array ("user3","user2","user1"));
$login = $db->getValue ("users", "login");
if ($login != "user3") {
echo "order by field test failed";
exit;
}
$db->where ("active", true);
$users = $db->get("users");
if ($db->count != 1) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment