Commit 252724ec authored by Alexander Butenko's avatar Alexander Butenko

Partical fix for orderBy with setPrefix() and table specification bug

parent 07078e0a
...@@ -553,6 +553,12 @@ class MysqliDb ...@@ -553,6 +553,12 @@ class MysqliDb
$orderbyDirection = strtoupper (trim ($orderbyDirection)); $orderbyDirection = strtoupper (trim ($orderbyDirection));
$orderByField = preg_replace ("/[^-a-z0-9\.\(\),_`]+/i",'', $orderByField); $orderByField = preg_replace ("/[^-a-z0-9\.\(\),_`]+/i",'', $orderByField);
// Add table prefix to orderByField if needed.
//FIXME: We are adding prefix only if table is enclosed into `` to distinguish aliases
// from table names
$orderByField = preg_replace('/(\`)([`a-zA-Z0-9_]*\.)/', '\1' . self::$prefix. '\2', $orderByField);
if (empty($orderbyDirection) || !in_array ($orderbyDirection, $allowedDirection)) if (empty($orderbyDirection) || !in_array ($orderbyDirection, $allowedDirection))
die ('Wrong order direction: '.$orderbyDirection); die ('Wrong order direction: '.$orderbyDirection);
......
...@@ -333,13 +333,27 @@ $results = $db->get('users'); ...@@ -333,13 +333,27 @@ $results = $db->get('users');
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND (); // Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
``` ```
order by values example: Order by values example:
```php ```php
$db->orderBy('userGroup', 'ASC', array('superuser', 'admin', 'users')); $db->orderBy('userGroup', 'ASC', array('superuser', 'admin', 'users'));
$db->get('users'); $db->get('users');
// Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC; // Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC;
``` ```
If you are using setPrefix () functionality and need to use table names in orderBy() method make sure that table names are escaped with ``.
```php
$db->setPrefix ("t_");
$db->orderBy ("users.id","asc");
$results = $db->get ('users');
// WRONG: That will give: SELECT * FROM t_users ORDER BY users.id ASC;
$db->setPrefix ("t_");
$db->orderBy ("`users`.id", "asc");
$results = $db->get ('users');
// CORRECT: That will give: SELECT * FROM t_users ORDER BY t_users.id ASC;
```
### Grouping method ### Grouping method
```php ```php
$db->groupBy ("name"); $db->groupBy ("name");
......
...@@ -136,7 +136,7 @@ if (!is_array ($products[0]['userId'])) { ...@@ -136,7 +136,7 @@ if (!is_array ($products[0]['userId'])) {
exit; exit;
} }
$depts = product::join('user')->orderBy('t_products.id', 'desc')->get(5); $depts = product::join('user')->orderBy('`products`.id', 'desc')->get(5);
foreach ($depts as $d) { foreach ($depts as $d) {
if (!is_object($d)) { if (!is_object($d)) {
echo "Return should be an object\n"; echo "Return should be an object\n";
...@@ -246,10 +246,10 @@ if (!user::byId(1) instanceof user) ...@@ -246,10 +246,10 @@ if (!user::byId(1) instanceof user)
if (!is_array (user::ArrayBuilder()->byId(1))) if (!is_array (user::ArrayBuilder()->byId(1)))
echo "wrong return type2"; echo "wrong return type2";
if (!is_array (product::join('user')->orderBy('t_products.id', 'desc')->get(2))) if (!is_array (product::join('user')->orderBy('`products`.id', 'desc')->get(2)))
echo "wrong return type2"; echo "wrong return type2";
if (!is_array (product::orderBy('t_products.id', 'desc')->join('user')->get(2))) if (!is_array (product::orderBy('`products`.id', 'desc')->join('user')->get(2)))
echo "wrong return type2"; echo "wrong return type2";
$u = new user; $u = new user;
...@@ -257,10 +257,10 @@ if (!$u->byId(1) instanceof user) ...@@ -257,10 +257,10 @@ if (!$u->byId(1) instanceof user)
echo "wrong return type2"; echo "wrong return type2";
$p = new product; $p = new product;
if (!is_array ($p->join('user')->orderBy('t_products.id', 'desc')->get(2))) if (!is_array ($p->join('user')->orderBy('`products`.id', 'desc')->get(2)))
echo "wrong return type2"; echo "wrong return type2";
if (!is_array ($p->orderBy('t_products.id', 'desc')->join('user')->get(2))) if (!is_array ($p->orderBy('`products`.id', 'desc')->join('user')->get(2)))
echo "wrong return type2"; echo "wrong return type2";
echo "All done"; echo "All done";
......
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