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

Bugfixes and API change

parent d2aa5131
<?php
abstract class dbObject {
class dbObject {
private $db;
public $data;
public $isNew = true;
public static $returnType = 'Object';
public $returnType = 'Object';
private $_with = Array();
public function __construct ($data = null) {
$this->db = MysqliDb::getInstance();
......@@ -16,31 +17,33 @@ abstract class dbObject {
}
public function __get ($name) {
if (property_exists ($this, 'relations')) {
if (isset ($this->relations[$name])) {
$relationType = strtolower ($this->relations[$name][0]);
$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, 'relations')) {
if (isset ($this->data[$name]))
return $this->data[$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) {
......@@ -112,9 +115,16 @@ abstract class dbObject {
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) {
if ($primaryKey)
$this->db->where ($this->primaryKey, $primaryKey);
$this->db->where ($this->dbTable . '.' . $this->primaryKey, $primaryKey);
$results = $this->db->getOne ($this->dbTable, $fields);
if (isset($this->jsonFields) && is_array($this->jsonFields)) {
......@@ -125,8 +135,10 @@ abstract class dbObject {
foreach ($this->arrayFields as $key)
$results[$key] = explode ("|", $results[$key]);
}
if (static::$returnType == 'Array')
if ($this->returnType == 'Array') {
$this->processWith ($results);
return $results;
}
$item = new static ($results);
$item->isNew = false;
......@@ -146,18 +158,25 @@ abstract class dbObject {
foreach ($this->arrayFields as $key)
$r[$key] = explode ("|", $r[$key]);
}
if (static::$returnType == 'Object') {
if ($this->returnType == 'Object') {
$item = new static ($r);
$item->isNew = false;
$objects[] = $item;
}
} else
$this->processWith($r);
}
if (static::$returnType == 'Object')
if ($this->returnType == 'Object')
return $objects;
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;
if (!$key)
$key = $objectName . "id";
......@@ -212,11 +231,12 @@ abstract class dbObject {
continue;
}
if (in_array ($key, $this->jsonFields))
if (isset ($this->jsonFields) && in_array ($key, $this->jsonFields))
$sqlData[$key] = json_encode($value);
else
else if (isset ($this->arrayFields) && in_array ($key, $this->arrayFields))
$sqlData[$key] = implode ("|", $value);
else
$sqlData[$key] = $value;
}
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");
* @property string authcode
* @property string iscallerid
*/
class department extends dbObject {
protected $dbTable = "departments";
class product extends dbObject {
protected $dbTable = "products";
protected $primaryKey = "id";
protected $dbFields = Array (
'userid' => 'int:required',
'name' => 'int:required',
'authcode' => 'int',
'userid' => 'int',
'iscallerid' => 'int',
'testvar' => 'int'
'userId' => 'int:required',
'customerId' => 'int:required',
'productName' => 'char:required'
);
protected $relations = Array (
'userid' => Array ("hasOne", "user")
'userId' => Array ("hasOne", "user")
);
protected $jsonFields = Array ('authcode');
public function last () {
$this->where ("id" , 130, '>');
return $this;
......
<?
require_once ("../MysqliDb.php");
require_once ("../dbObject.php");
require_once ("models/department.php");
$db = new Mysqlidb('localhost', 'root', '', 'akorbi');
$dept4 = department::ArrayBuilder()->join('user')->get(2);
echo json_encode ($dept4);
$dept = new department();
$dept->userid = 10;
$dept->name = 'avb test';
$dept->authcode = Array('1234','123456');
$dept->iscallerid = 1;
$dept->insert();
echo "ID = " . $dept->id . "\n";
$dept2 = new department([
'userid' => '11',
'name' => 'john doe',
'authcode' => '5678',
'iscallerid' => 0,
]);
$dept2->save();
$dept2->iscallerid=1;
print_r ($dept2->data);
$dept2->save();
echo "department is of class " . get_class ($dept2) . "\n";
echo "List\n";
$depts = department::get ();
require_once ("models/product.php");
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
$tables = Array (
'users' => Array (
'login' => 'char(10) not null',
'active' => 'bool default 0',
'customerId' => 'int(10) not null',
'firstName' => 'char(10) not null',
'lastName' => 'char(10)',
'password' => 'text not null',
'createdAt' => 'datetime',
'expires' => 'datetime',
'loginCount' => 'int(10) default 0'
),
'products' => Array (
'customerId' => 'int(10) not null',
'userId' => 'int(10) not null',
'productName' => 'char(50)'
)
);
$data = Array (
'user' => Array (
Array ('login' => 'user1',
'customerId' => 10,
'firstName' => 'John',
'lastName' => 'Doe',
'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) {
// print_r ($d->data);
echo $d . "\n";
echo "department is of class " . get_class ($d) . "\n";
if (!is_object($d)) {
echo "Return should be an object\n";
exit;
}
}
echo "getOne\n";
$dept3 = department::byId ($dept->id);
echo 'cnt ' . $dept3->count . "\n";
$dept3->authcode=443;
$dept3->save();
print_r ($dept3->data) . "\n";
echo "department is of class " . get_class ($dept3) . "\n";
$dept = product::join('user')->byId(5);
if (count ($dept->data) != 12) {
echo "wrong props count " .count ($dept->data). "\n";
exit;
}
echo "hasOne\n";
echo json_encode ($dept3->userid->data);
echo "user is of class " . get_class ($dept3->userid) . "\n";
// hasOne
$products = product::get ();
$cnt = 0;
foreach ($products as $p) {
if (get_class ($d) != 'product') {
echo "wrong class returned\n";
exit;
}
echo "\nhasMany\n";
foreach ($dept3->userid->departments as $d) {
echo $d;
echo "department is of class " . get_class ($d) . "\n";
if (!($p->userId instanceof user)) {
echo "wrong return class of hasOne result\n";
exit;
}
$cnt++;
}
$user = user::byId (41);
echo "user is of class " . get_class ($user) . "\n";
if (($cnt != $db->count) && ($cnt != 5)) {
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 (
)
);
$data = Array (
'users' => Array (
'user' => Array (
Array ('login' => 'user1',
'customerId' => 10,
'firstName' => 'John',
......@@ -70,7 +70,7 @@ $data = Array (
'loginCount' => $db->inc(3)
)
),
'products' => Array (
'product' => Array (
Array ('customerId' => 1,
'userId' => 1,
'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