Skip to content

fixes #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2015
Merged

fixes #243

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MysqliDb
*
* @var string
*/
protected static $_prefix;
public static $prefix;
/**
* MySQLi instance
*
Expand Down Expand Up @@ -220,7 +220,7 @@ protected function reset()
*/
public function setPrefix($prefix = '')
{
self::$_prefix = $prefix;
self::$prefix = $prefix;
return $this;
}

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

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

if ($this->isSubQuery)
Expand Down Expand Up @@ -396,7 +396,7 @@ public function insert($tableName, $insertData)
if ($this->isSubQuery)
return;

$this->_query = "INSERT INTO " .self::$_prefix . $tableName;
$this->_query = "INSERT INTO " .self::$prefix . $tableName;
$stmt = $this->_buildQuery(null, $insertData);
$stmt->execute();
$this->_stmtError = $stmt->error;
Expand Down Expand Up @@ -439,7 +439,7 @@ public function update($tableName, $tableData)
if ($this->isSubQuery)
return;

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

$stmt = $this->_buildQuery (null, $tableData);
$status = $stmt->execute();
Expand All @@ -464,7 +464,7 @@ public function delete($tableName, $numRows = null)
if ($this->isSubQuery)
return;

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

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

if (!is_object ($joinTable))
$joinTable = self::$_prefix . filter_var($joinTable, FILTER_SANITIZE_STRING);
$joinTable = self::$prefix . filter_var($joinTable, FILTER_SANITIZE_STRING);

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

Expand Down
7 changes: 4 additions & 3 deletions dbObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public function delete () {
* @return dbObject|array
*/
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);
}

Expand Down Expand Up @@ -365,7 +365,8 @@ private function join ($objectName, $key = null, $joinType = 'LEFT') {
$joinObj = new $objectName;
if (!$key)
$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);
return $this;
}
Expand Down Expand Up @@ -601,7 +602,7 @@ private static function dbObjectAutoload ($classname) {
*
* Calling autoload() without path will set path to dbObjectPath/models/ directory
*
* @param string $path
* @param string $path
*/
public static function autoload ($path = null) {
if ($path)
Expand Down
16 changes: 9 additions & 7 deletions tests/dbObjectTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require_once ("../dbObject.php");

$db = new Mysqlidb('localhost', 'root', '', 'testdb');
$prefix = 't_';
$db->setPrefix($prefix);
dbObject::autoload ("models");

$tables = Array (
Expand Down Expand Up @@ -91,8 +93,8 @@ function createTable ($name, $data) {

// rawQuery test
foreach ($tables as $name => $fields) {
$db->rawQuery("DROP TABLE " . $name);
createTable ($name, $fields);
$db->rawQuery("DROP TABLE " . $prefix . $name);
createTable ($prefix . $name, $fields);
}

foreach ($data as $name => $datas) {
Expand Down Expand Up @@ -134,7 +136,7 @@ function createTable ($name, $data) {
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) {
if (!is_object($d)) {
echo "Return should be an object\n";
Expand Down Expand Up @@ -244,21 +246,21 @@ function createTable ($name, $data) {
if (!is_array (user::ArrayBuilder()->byId(1)))
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";

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";

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

$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";

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 "All done";
Expand Down