Skip to content

Commit bc28639

Browse files
committed
Merge pull request #244 from avbdr/master
Partical fix for orderBy with setPrefix() and table specification bug
2 parents daa0198 + 252724e commit bc28639

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

MysqliDb.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields
553553
$orderbyDirection = strtoupper (trim ($orderbyDirection));
554554
$orderByField = preg_replace ("/[^-a-z0-9\.\(\),_`]+/i",'', $orderByField);
555555

556+
// Add table prefix to orderByField if needed.
557+
//FIXME: We are adding prefix only if table is enclosed into `` to distinguish aliases
558+
// from table names
559+
$orderByField = preg_replace('/(\`)([`a-zA-Z0-9_]*\.)/', '\1' . self::$prefix. '\2', $orderByField);
560+
561+
556562
if (empty($orderbyDirection) || !in_array ($orderbyDirection, $allowedDirection))
557563
die ('Wrong order direction: '.$orderbyDirection);
558564

readme.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,27 @@ $results = $db->get('users');
333333
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
334334
```
335335

336-
order by values example:
336+
Order by values example:
337337
```php
338338
$db->orderBy('userGroup', 'ASC', array('superuser', 'admin', 'users'));
339339
$db->get('users');
340340
// Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC;
341341
```
342342

343+
If you are using setPrefix () functionality and need to use table names in orderBy() method make sure that table names are escaped with ``.
344+
345+
```php
346+
$db->setPrefix ("t_");
347+
$db->orderBy ("users.id","asc");
348+
$results = $db->get ('users');
349+
// WRONG: That will give: SELECT * FROM t_users ORDER BY users.id ASC;
350+
351+
$db->setPrefix ("t_");
352+
$db->orderBy ("`users`.id", "asc");
353+
$results = $db->get ('users');
354+
// CORRECT: That will give: SELECT * FROM t_users ORDER BY t_users.id ASC;
355+
```
356+
343357
### Grouping method
344358
```php
345359
$db->groupBy ("name");

tests/dbObjectTests.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function createTable ($name, $data) {
136136
exit;
137137
}
138138

139-
$depts = product::join('user')->orderBy('t_products.id', 'desc')->get(5);
139+
$depts = product::join('user')->orderBy('`products`.id', 'desc')->get(5);
140140
foreach ($depts as $d) {
141141
if (!is_object($d)) {
142142
echo "Return should be an object\n";
@@ -246,21 +246,21 @@ function createTable ($name, $data) {
246246
if (!is_array (user::ArrayBuilder()->byId(1)))
247247
echo "wrong return type2";
248248

249-
if (!is_array (product::join('user')->orderBy('t_products.id', 'desc')->get(2)))
249+
if (!is_array (product::join('user')->orderBy('`products`.id', 'desc')->get(2)))
250250
echo "wrong return type2";
251251

252-
if (!is_array (product::orderBy('t_products.id', 'desc')->join('user')->get(2)))
252+
if (!is_array (product::orderBy('`products`.id', 'desc')->join('user')->get(2)))
253253
echo "wrong return type2";
254254

255255
$u = new user;
256256
if (!$u->byId(1) instanceof user)
257257
echo "wrong return type2";
258258

259259
$p = new product;
260-
if (!is_array ($p->join('user')->orderBy('t_products.id', 'desc')->get(2)))
260+
if (!is_array ($p->join('user')->orderBy('`products`.id', 'desc')->get(2)))
261261
echo "wrong return type2";
262262

263-
if (!is_array ($p->orderBy('t_products.id', 'desc')->join('user')->get(2)))
263+
if (!is_array ($p->orderBy('`products`.id', 'desc')->join('user')->get(2)))
264264
echo "wrong return type2";
265265

266266
echo "All done";

0 commit comments

Comments
 (0)