Commit 5350b8d8 authored by Alexander Butenko's avatar Alexander Butenko

Bugfixes and API change

parent d2aa5131
<?php <?php
abstract class dbObject { class dbObject {
private $db; private $db;
public $data; public $data;
public $isNew = true; public $isNew = true;
public static $returnType = 'Object'; public $returnType = 'Object';
private $_with = Array();
public function __construct ($data = null) { public function __construct ($data = null) {
$this->db = MysqliDb::getInstance(); $this->db = MysqliDb::getInstance();
...@@ -16,31 +17,33 @@ abstract class dbObject { ...@@ -16,31 +17,33 @@ abstract class dbObject {
} }
public function __get ($name) { public function __get ($name) {
if (property_exists ($this, 'relations')) { if (!property_exists ($this, 'relations')) {
if (isset ($this->relations[$name])) { if (isset ($this->data[$name]))
$relationType = strtolower ($this->relations[$name][0]); return $this->data[$name];
$modelName = $this->relations[$name][1];
switch ($relationType) {
case 'hasone':
return $modelName::ObjectBuilder()->byId($this->data[$name]);
break;
case 'hasmany':
$key = $this->relations[$name][2];
return $modelName::ObjectBuilder()->where($key, $this->data[$this->primaryKey])->get();
break;
default:
break;
}
}
}
if (isset ($this->data[$name]))
return $this->data[$name];
if (property_exists ($this->db, $name))
return $this->db->$name;
if (property_exists ($this->db, $name))
return $this->db->$name;
}
if (!isset ($this->relations[$name]))
return;
$relationType = strtolower ($this->relations[$name][0]);
$modelName = $this->relations[$name][1];
switch ($relationType) {
case 'hasone':
$obj = new $modelName;
$obj->returnType = $this->returnType;
return $obj->byId($this->data[$name]);
break;
case 'hasmany':
$key = $this->relations[$name][2];
$obj = new $modelName;
$obj->returnType = $this->returnType;
return $obj->where($key, $this->data[$this->primaryKey])->get();
break;
default:
break;
}
} }
public function __isset ($name) { public function __isset ($name) {
...@@ -112,9 +115,16 @@ abstract class dbObject { ...@@ -112,9 +115,16 @@ abstract class dbObject {
return $this->getOne ($fields, $id); return $this->getOne ($fields, $id);
} }
private function processWith (&$data) {
if (count ($this->_with) == 0)
return;
foreach ($this->_with as $w)
$data[$w] = $this->$w;
}
private function getOne ($fields = null, $primaryKey = null) { private function getOne ($fields = null, $primaryKey = null) {
if ($primaryKey) if ($primaryKey)
$this->db->where ($this->primaryKey, $primaryKey); $this->db->where ($this->dbTable . '.' . $this->primaryKey, $primaryKey);
$results = $this->db->getOne ($this->dbTable, $fields); $results = $this->db->getOne ($this->dbTable, $fields);
if (isset($this->jsonFields) && is_array($this->jsonFields)) { if (isset($this->jsonFields) && is_array($this->jsonFields)) {
...@@ -125,8 +135,10 @@ abstract class dbObject { ...@@ -125,8 +135,10 @@ abstract class dbObject {
foreach ($this->arrayFields as $key) foreach ($this->arrayFields as $key)
$results[$key] = explode ("|", $results[$key]); $results[$key] = explode ("|", $results[$key]);
} }
if (static::$returnType == 'Array') if ($this->returnType == 'Array') {
$this->processWith ($results);
return $results; return $results;
}
$item = new static ($results); $item = new static ($results);
$item->isNew = false; $item->isNew = false;
...@@ -146,18 +158,25 @@ abstract class dbObject { ...@@ -146,18 +158,25 @@ abstract class dbObject {
foreach ($this->arrayFields as $key) foreach ($this->arrayFields as $key)
$r[$key] = explode ("|", $r[$key]); $r[$key] = explode ("|", $r[$key]);
} }
if (static::$returnType == 'Object') { if ($this->returnType == 'Object') {
$item = new static ($r); $item = new static ($r);
$item->isNew = false; $item->isNew = false;
$objects[] = $item; $objects[] = $item;
} } else
$this->processWith($r);
} }
if (static::$returnType == 'Object') if ($this->returnType == 'Object')
return $objects; return $objects;
return $results; return $results;
} }
public function join ($objectName, $key = null, $joinType = 'LEFT') { private function with ($objectName) {
$this->_with[] = $objectName;
return $this;
}
private function join ($objectName, $key = null, $joinType = 'LEFT') {
$joinObj = new $objectName; $joinObj = new $objectName;
if (!$key) if (!$key)
$key = $objectName . "id"; $key = $objectName . "id";
...@@ -212,11 +231,12 @@ abstract class dbObject { ...@@ -212,11 +231,12 @@ abstract class dbObject {
continue; continue;
} }
if (in_array ($key, $this->jsonFields)) if (isset ($this->jsonFields) && in_array ($key, $this->jsonFields))
$sqlData[$key] = json_encode($value); $sqlData[$key] = json_encode($value);
else else if (isset ($this->arrayFields) && in_array ($key, $this->arrayFields))
$sqlData[$key] = implode ("|", $value); $sqlData[$key] = implode ("|", $value);
else
$sqlData[$key] = $value;
} }
return $sqlData; return $sqlData;
} }
......
<?php
require_once "../dbObject.php";
class accData extends dbObject {
protected $dbTable = "acc_data";
protected $primaryKey = "id";
public $calldate;
public $callid;
public $clientid;
public $queueid;
public function last () {
$this->where ("id" , 1300, '>');
return $this;
}
}
?>
...@@ -10,23 +10,18 @@ require_once ("user.php"); ...@@ -10,23 +10,18 @@ require_once ("user.php");
* @property string authcode * @property string authcode
* @property string iscallerid * @property string iscallerid
*/ */
class department extends dbObject { class product extends dbObject {
protected $dbTable = "departments"; protected $dbTable = "products";
protected $primaryKey = "id"; protected $primaryKey = "id";
protected $dbFields = Array ( protected $dbFields = Array (
'userid' => 'int:required', 'userId' => 'int:required',
'name' => 'int:required', 'customerId' => 'int:required',
'authcode' => 'int', 'productName' => 'char:required'
'userid' => 'int',
'iscallerid' => 'int',
'testvar' => 'int'
); );
protected $relations = Array ( protected $relations = Array (
'userid' => Array ("hasOne", "user") 'userId' => Array ("hasOne", "user")
); );
protected $jsonFields = Array ('authcode');
public function last () { public function last () {
$this->where ("id" , 130, '>'); $this->where ("id" , 130, '>');
return $this; return $this;
......
<? <?
require_once ("../MysqliDb.php"); require_once ("../MysqliDb.php");
require_once ("../dbObject.php"); require_once ("../dbObject.php");
require_once ("models/department.php"); require_once ("models/product.php");
$db = new Mysqlidb('localhost', 'root', '', 'akorbi'); $db = new Mysqlidb('localhost', 'root', '', 'testdb');
$tables = Array (
$dept4 = department::ArrayBuilder()->join('user')->get(2); 'users' => Array (
echo json_encode ($dept4); 'login' => 'char(10) not null',
'active' => 'bool default 0',
$dept = new department(); 'customerId' => 'int(10) not null',
$dept->userid = 10; 'firstName' => 'char(10) not null',
$dept->name = 'avb test'; 'lastName' => 'char(10)',
$dept->authcode = Array('1234','123456'); 'password' => 'text not null',
$dept->iscallerid = 1; 'createdAt' => 'datetime',
$dept->insert(); 'expires' => 'datetime',
echo "ID = " . $dept->id . "\n"; 'loginCount' => 'int(10) default 0'
),
$dept2 = new department([ 'products' => Array (
'userid' => '11', 'customerId' => 'int(10) not null',
'name' => 'john doe', 'userId' => 'int(10) not null',
'authcode' => '5678', 'productName' => 'char(50)'
'iscallerid' => 0, )
]); );
$dept2->save();
$dept2->iscallerid=1; $data = Array (
print_r ($dept2->data); 'user' => Array (
$dept2->save(); Array ('login' => 'user1',
echo "department is of class " . get_class ($dept2) . "\n"; 'customerId' => 10,
'firstName' => 'John',
echo "List\n"; 'lastName' => 'Doe',
$depts = department::get (); 'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y'),
'loginCount' => $db->inc()
),
Array ('login' => 'user2',
'customerId' => 10,
'firstName' => 'Mike',
'lastName' => NULL,
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y'),
'loginCount' => $db->inc(2)
),
Array ('login' => 'user3',
'active' => true,
'customerId' => 11,
'firstName' => 'Pete',
'lastName' => 'D',
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y'),
'loginCount' => $db->inc(3)
)
),
'product' => Array (
Array ('customerId' => 1,
'userId' => 1,
'productName' => 'product1',
),
Array ('customerId' => 1,
'userId' => 1,
'productName' => 'product2',
),
Array ('customerId' => 1,
'userId' => 1,
'productName' => 'product3',
),
Array ('customerId' => 1,
'userId' => 2,
'productName' => 'product4',
),
Array ('customerId' => 1,
'userId' => 2,
'productName' => 'product5',
),
)
);
function createTable ($name, $data) {
global $db;
//$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT";
foreach ($data as $k => $v) {
$q .= ", $k $v";
}
$q .= ")";
$db->rawQuery($q);
}
// rawQuery test
foreach ($tables as $name => $fields) {
$db->rawQuery("DROP TABLE " . $name);
createTable ($name, $fields);
}
foreach ($data as $name => $datas) {
foreach ($data[$name] as $userData) {
$obj = new $name ($userData);
$id = $obj->save();
echo "$name $id created\n";
}
}
$products = product::ArrayBuilder()->get(2);
foreach ($products as $p) {
if (!is_array ($p)) {
echo "ArrayBuilder do not return an array\n";
exit;
}
}
$products = product::ArrayBuilder()->get(2);
$products = product::ArrayBuilder()->with('userId')->get(2);
print_r ($products);
$depts = product::join('user')->orderBy('products.id', 'desc')->get(5);
foreach ($depts as $d) { foreach ($depts as $d) {
// print_r ($d->data); if (!is_object($d)) {
echo $d . "\n"; echo "Return should be an object\n";
echo "department is of class " . get_class ($d) . "\n"; exit;
}
} }
echo "getOne\n"; $dept = product::join('user')->byId(5);
$dept3 = department::byId ($dept->id); if (count ($dept->data) != 12) {
echo 'cnt ' . $dept3->count . "\n"; echo "wrong props count " .count ($dept->data). "\n";
$dept3->authcode=443; exit;
$dept3->save(); }
print_r ($dept3->data) . "\n";
echo "department is of class " . get_class ($dept3) . "\n";
echo "hasOne\n"; // hasOne
echo json_encode ($dept3->userid->data); $products = product::get ();
echo "user is of class " . get_class ($dept3->userid) . "\n"; $cnt = 0;
foreach ($products as $p) {
if (get_class ($d) != 'product') {
echo "wrong class returned\n";
exit;
}
echo "\nhasMany\n"; if (!($p->userId instanceof user)) {
foreach ($dept3->userid->departments as $d) { echo "wrong return class of hasOne result\n";
echo $d; exit;
echo "department is of class " . get_class ($d) . "\n"; }
$cnt++;
} }
$user = user::byId (41); if (($cnt != $db->count) && ($cnt != 5)) {
echo "user is of class " . get_class ($user) . "\n"; echo "wrong count after get\n";
exit;
}
// hasMany
$user = user::where('id',1)->getOne();
if (!is_array ($user->products) || (count ($user->products) != 3)) {
echo "wrong count in hasMany\n";
exit;
}
foreach ($user->products as $p) {
if (!($p instanceof product)) {
echo "wrong return class of hasMany result\n";
exit;
}
}
?> ?>
...@@ -40,7 +40,7 @@ $tables = Array ( ...@@ -40,7 +40,7 @@ $tables = Array (
) )
); );
$data = Array ( $data = Array (
'users' => Array ( 'user' => Array (
Array ('login' => 'user1', Array ('login' => 'user1',
'customerId' => 10, 'customerId' => 10,
'firstName' => 'John', 'firstName' => 'John',
...@@ -70,7 +70,7 @@ $data = Array ( ...@@ -70,7 +70,7 @@ $data = Array (
'loginCount' => $db->inc(3) 'loginCount' => $db->inc(3)
) )
), ),
'products' => Array ( 'product' => Array (
Array ('customerId' => 1, Array ('customerId' => 1,
'userId' => 1, 'userId' => 1,
'productName' => 'product1', 'productName' => 'product1',
......
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