Skip to content

Commit daa0198

Browse files
committed
Merge pull request #243 from avbdr/master
fixes
2 parents 7a14d1b + 07078e0 commit daa0198

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

MysqliDb.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MysqliDb
2424
*
2525
* @var string
2626
*/
27-
protected static $_prefix;
27+
public static $prefix;
2828
/**
2929
* MySQLi instance
3030
*
@@ -220,7 +220,7 @@ protected function reset()
220220
*/
221221
public function setPrefix($prefix = '')
222222
{
223-
self::$_prefix = $prefix;
223+
self::$prefix = $prefix;
224224
return $this;
225225
}
226226

@@ -333,7 +333,7 @@ public function get($tableName, $numRows = null, $columns = '*')
333333

334334
$column = is_array($columns) ? implode(', ', $columns) : $columns;
335335
$this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' .
336-
$column . " FROM " .self::$_prefix . $tableName;
336+
$column . " FROM " .self::$prefix . $tableName;
337337
$stmt = $this->_buildQuery($numRows);
338338

339339
if ($this->isSubQuery)
@@ -396,7 +396,7 @@ public function insert($tableName, $insertData)
396396
if ($this->isSubQuery)
397397
return;
398398

399-
$this->_query = "INSERT INTO " .self::$_prefix . $tableName;
399+
$this->_query = "INSERT INTO " .self::$prefix . $tableName;
400400
$stmt = $this->_buildQuery(null, $insertData);
401401
$stmt->execute();
402402
$this->_stmtError = $stmt->error;
@@ -439,7 +439,7 @@ public function update($tableName, $tableData)
439439
if ($this->isSubQuery)
440440
return;
441441

442-
$this->_query = "UPDATE " . self::$_prefix . $tableName;
442+
$this->_query = "UPDATE " . self::$prefix . $tableName;
443443

444444
$stmt = $this->_buildQuery (null, $tableData);
445445
$status = $stmt->execute();
@@ -464,7 +464,7 @@ public function delete($tableName, $numRows = null)
464464
if ($this->isSubQuery)
465465
return;
466466

467-
$this->_query = "DELETE FROM " . self::$_prefix . $tableName;
467+
$this->_query = "DELETE FROM " . self::$prefix . $tableName;
468468

469469
$stmt = $this->_buildQuery($numRows);
470470
$stmt->execute();
@@ -531,7 +531,7 @@ public function join($joinTable, $joinCondition, $joinType = '')
531531
die ('Wrong JOIN type: '.$joinType);
532532

533533
if (!is_object ($joinTable))
534-
$joinTable = self::$_prefix . filter_var($joinTable, FILTER_SANITIZE_STRING);
534+
$joinTable = self::$prefix . filter_var($joinTable, FILTER_SANITIZE_STRING);
535535

536536
$this->_join[] = Array ($joinType, $joinTable, $joinCondition);
537537

dbObject.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public function delete () {
282282
* @return dbObject|array
283283
*/
284284
private function byId ($id, $fields = null) {
285-
$this->db->where ($this->dbTable . '.' . $this->primaryKey, $id);
285+
$this->db->where (MysqliDb::$prefix . $this->dbTable . '.' . $this->primaryKey, $id);
286286
return $this->getOne ($fields);
287287
}
288288

@@ -365,7 +365,8 @@ private function join ($objectName, $key = null, $joinType = 'LEFT') {
365365
$joinObj = new $objectName;
366366
if (!$key)
367367
$key = $objectName . "id";
368-
$joinStr = "{$this->dbTable}.{$key} = {$joinObj->dbTable}.{$joinObj->primaryKey}";
368+
$joinStr = MysqliDb::$prefix . $this->dbTable . ".{$key} = " .
369+
MysqliDb::$prefix . "{$joinObj->dbTable}.{$joinObj->primaryKey}";
369370
$this->db->join ($joinObj->dbTable, $joinStr, $joinType);
370371
return $this;
371372
}
@@ -601,7 +602,7 @@ private static function dbObjectAutoload ($classname) {
601602
*
602603
* Calling autoload() without path will set path to dbObjectPath/models/ directory
603604
*
604-
* @param string $path
605+
* @param string $path
605606
*/
606607
public static function autoload ($path = null) {
607608
if ($path)

tests/dbObjectTests.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require_once ("../dbObject.php");
55

66
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
7+
$prefix = 't_';
8+
$db->setPrefix($prefix);
79
dbObject::autoload ("models");
810

911
$tables = Array (
@@ -91,8 +93,8 @@ function createTable ($name, $data) {
9193

9294
// rawQuery test
9395
foreach ($tables as $name => $fields) {
94-
$db->rawQuery("DROP TABLE " . $name);
95-
createTable ($name, $fields);
96+
$db->rawQuery("DROP TABLE " . $prefix . $name);
97+
createTable ($prefix . $name, $fields);
9698
}
9799

98100
foreach ($data as $name => $datas) {
@@ -134,7 +136,7 @@ function createTable ($name, $data) {
134136
exit;
135137
}
136138

137-
$depts = product::join('user')->orderBy('products.id', 'desc')->get(5);
139+
$depts = product::join('user')->orderBy('t_products.id', 'desc')->get(5);
138140
foreach ($depts as $d) {
139141
if (!is_object($d)) {
140142
echo "Return should be an object\n";
@@ -244,21 +246,21 @@ function createTable ($name, $data) {
244246
if (!is_array (user::ArrayBuilder()->byId(1)))
245247
echo "wrong return type2";
246248

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

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

253255
$u = new user;
254256
if (!$u->byId(1) instanceof user)
255257
echo "wrong return type2";
256258

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

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

264266
echo "All done";

0 commit comments

Comments
 (0)