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
$orderbyDirection = strtoupper (trim ($orderbyDirection));
$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))
die ('Wrong order direction: '.$orderbyDirection);
......
......@@ -333,13 +333,27 @@ $results = $db->get('users');
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
```
order by values example:
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;
```
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
```php
$db->groupBy ("name");
......
......@@ -136,7 +136,7 @@ if (!is_array ($products[0]['userId'])) {
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) {
if (!is_object($d)) {
echo "Return should be an object\n";
......@@ -246,10 +246,10 @@ if (!user::byId(1) instanceof user)
if (!is_array (user::ArrayBuilder()->byId(1)))
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";
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";
$u = new user;
......@@ -257,10 +257,10 @@ if (!$u->byId(1) instanceof user)
echo "wrong return type2";
$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";
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 "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