Commit 07078e0a authored by Alexander Butenko's avatar Alexander Butenko

Partical fix for a dbObject usage with setPrefix()

parent 7ec9f667
...@@ -282,7 +282,7 @@ class dbObject { ...@@ -282,7 +282,7 @@ class dbObject {
* @return dbObject|array * @return dbObject|array
*/ */
private function byId ($id, $fields = null) { private function byId ($id, $fields = null) {
$this->db->where ($this->dbTable . '.' . $this->primaryKey, $id); $this->db->where (MysqliDb::$prefix . $this->dbTable . '.' . $this->primaryKey, $id);
return $this->getOne ($fields); return $this->getOne ($fields);
} }
...@@ -365,7 +365,8 @@ class dbObject { ...@@ -365,7 +365,8 @@ class dbObject {
$joinObj = new $objectName; $joinObj = new $objectName;
if (!$key) if (!$key)
$key = $objectName . "id"; $key = $objectName . "id";
$joinStr = "{$this->dbTable}.{$key} = {$joinObj->dbTable}.{$joinObj->primaryKey}"; $joinStr = MysqliDb::$prefix . $this->dbTable . ".{$key} = " .
MysqliDb::$prefix . "{$joinObj->dbTable}.{$joinObj->primaryKey}";
$this->db->join ($joinObj->dbTable, $joinStr, $joinType); $this->db->join ($joinObj->dbTable, $joinStr, $joinType);
return $this; return $this;
} }
...@@ -601,7 +602,7 @@ class dbObject { ...@@ -601,7 +602,7 @@ class dbObject {
* *
* Calling autoload() without path will set path to dbObjectPath/models/ directory * Calling autoload() without path will set path to dbObjectPath/models/ directory
* *
* @param string $path * @param string $path
*/ */
public static function autoload ($path = null) { public static function autoload ($path = null) {
if ($path) if ($path)
......
...@@ -4,6 +4,8 @@ require_once ("../MysqliDb.php"); ...@@ -4,6 +4,8 @@ require_once ("../MysqliDb.php");
require_once ("../dbObject.php"); require_once ("../dbObject.php");
$db = new Mysqlidb('localhost', 'root', '', 'testdb'); $db = new Mysqlidb('localhost', 'root', '', 'testdb');
$prefix = 't_';
$db->setPrefix($prefix);
dbObject::autoload ("models"); dbObject::autoload ("models");
$tables = Array ( $tables = Array (
...@@ -91,8 +93,8 @@ function createTable ($name, $data) { ...@@ -91,8 +93,8 @@ function createTable ($name, $data) {
// rawQuery test // rawQuery test
foreach ($tables as $name => $fields) { foreach ($tables as $name => $fields) {
$db->rawQuery("DROP TABLE " . $name); $db->rawQuery("DROP TABLE " . $prefix . $name);
createTable ($name, $fields); createTable ($prefix . $name, $fields);
} }
foreach ($data as $name => $datas) { foreach ($data as $name => $datas) {
...@@ -134,7 +136,7 @@ if (!is_array ($products[0]['userId'])) { ...@@ -134,7 +136,7 @@ if (!is_array ($products[0]['userId'])) {
exit; exit;
} }
$depts = product::join('user')->orderBy('products.id', 'desc')->get(5); $depts = product::join('user')->orderBy('t_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";
...@@ -244,10 +246,10 @@ if (!user::byId(1) instanceof user) ...@@ -244,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('products.id', 'desc')->get(2))) if (!is_array (product::join('user')->orderBy('t_products.id', 'desc')->get(2)))
echo "wrong return type2"; echo "wrong return type2";
if (!is_array (product::orderBy('products.id', 'desc')->join('user')->get(2))) if (!is_array (product::orderBy('t_products.id', 'desc')->join('user')->get(2)))
echo "wrong return type2"; echo "wrong return type2";
$u = new user; $u = new user;
...@@ -255,10 +257,10 @@ if (!$u->byId(1) instanceof user) ...@@ -255,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('products.id', 'desc')->get(2))) if (!is_array ($p->join('user')->orderBy('t_products.id', 'desc')->get(2)))
echo "wrong return type2"; echo "wrong return type2";
if (!is_array ($p->orderBy('products.id', 'desc')->join('user')->get(2))) if (!is_array ($p->orderBy('t_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