Commit d2aa5131 authored by Alexander Butenko's avatar Alexander Butenko

API change

parent 73bd522a
......@@ -22,7 +22,7 @@ abstract class dbObject {
$modelName = $this->relations[$name][1];
switch ($relationType) {
case 'hasone':
return $modelName::byId($this->data[$name]);
return $modelName::ObjectBuilder()->byId($this->data[$name]);
break;
case 'hasmany':
$key = $this->relations[$name][2];
......@@ -44,7 +44,7 @@ abstract class dbObject {
}
public function __isset ($name) {
if ($this->data[$name])
if (isset ($this->data[$name]))
return isset ($this->data[$name]);
if (property_exists ($this->db, $name))
......@@ -108,22 +108,21 @@ abstract class dbObject {
}
public static function byId ($id, $fields = null) {
return static::getOne ($fields, $id);
private function byId ($id, $fields = null) {
return $this->getOne ($fields, $id);
}
public static function getOne ($fields = null, $primaryKey = null, $obj = null) {
$obj = new static;
private function getOne ($fields = null, $primaryKey = null) {
if ($primaryKey)
$obj->db->where ($obj->primaryKey, $primaryKey);
$this->db->where ($this->primaryKey, $primaryKey);
$results = $obj->db->getOne ($obj->dbTable, $fields);
if (isset($obj->jsonFields) && is_array($obj->jsonFields)) {
foreach ($obj->jsonFields as $key)
$results = $this->db->getOne ($this->dbTable, $fields);
if (isset($this->jsonFields) && is_array($this->jsonFields)) {
foreach ($this->jsonFields as $key)
$results[$key] = json_decode ($results[$key]);
}
if (isset($obj->arrayFields) && is_array($obj->arrayFields)) {
foreach ($obj->arrayFields as $key)
if (isset($this->arrayFields) && is_array($this->arrayFields)) {
foreach ($this->arrayFields as $key)
$results[$key] = explode ("|", $results[$key]);
}
if (static::$returnType == 'Array')
......@@ -135,17 +134,16 @@ abstract class dbObject {
return $item;
}
public static function get ($limit = null, $fields = null) {
$obj = new static;
private function get ($limit = null, $fields = null) {
$objects = Array ();
$results = $obj->db->get($obj->dbTable, $limit, $fields);
$results = $this->db->get ($this->dbTable, $limit, $fields);
foreach ($results as &$r) {
if (isset ($obj->jsonFields) && is_array($obj->jsonFields)) {
foreach ($obj->jsonFields as $key)
if (isset ($this->jsonFields) && is_array($this->jsonFields)) {
foreach ($this->jsonFields as $key)
$r[$key] = json_decode ($r[$key]);
}
if (isset ($obj->arrayFields) && is_array($obj->arrayFields)) {
foreach ($obj->arrayFields as $key)
if (isset ($this->arrayFields) && is_array($this->arrayFields)) {
foreach ($this->arrayFields as $key)
$r[$key] = explode ("|", $r[$key]);
}
if (static::$returnType == 'Object') {
......@@ -173,19 +171,22 @@ abstract class dbObject {
return $res['cnt'];
}
public function __call ($method, $arg) {
if (method_exists ($this, $method))
return call_user_func_array (array ($this, $method), $arg);
call_user_func_array (array ($this->db, $method), $arg);
return $this;
}
public static function __callStatic ($method, $arg) {
$obj = new static;
call_user_func_array (array ($obj, $method), $arg);
$result = call_user_func_array (array ($obj, $method), $arg);
if (method_exists ($obj, $method))
return $result;
return $obj;
}
public function toJson () {
return json_encode ($this->data);
}
......
......@@ -14,6 +14,7 @@ $dept->name = 'avb test';
$dept->authcode = Array('1234','123456');
$dept->iscallerid = 1;
$dept->insert();
echo "ID = " . $dept->id . "\n";
$dept2 = new department([
'userid' => '11',
......@@ -25,29 +26,36 @@ $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 ();
foreach ($depts as $d) {
// print_r ($d->data);
echo $d . "\n";
echo "department is of class " . get_class ($d) . "\n";
}
echo "getOne\n";
$dept3 = department::byId ("181");
$dept3 = department::byId ($dept->id);
echo 'cnt ' . $dept3->count . "\n";
$dept3->authcode=333;
$dept3->authcode=443;
$dept3->save();
print_r ($dept3->data) . "\n";
echo "department is of class " . get_class ($dept3) . "\n";
echo "hasOne\n";
echo json_encode ($dept3->userid->data);
echo "user is of class " . get_class ($dept3->userid) . "\n";
echo "\nhasMany\n";
foreach ($dept3->userid->departments as $d) {
echo $d;
echo $d;
echo "department is of class " . get_class ($d) . "\n";
}
$user = user::byId (41);
echo "user is of class " . get_class ($user) . "\n";
?>
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