Skip to content

Commit 07078e0

Browse files
committed
Partical fix for a dbObject usage with setPrefix()
1 parent 7ec9f66 commit 07078e0

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

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)