Commit 35a43d03 authored by Alexander Butenko's avatar Alexander Butenko

First real api

parent 79a83d07
<?php
abstract class dbObject {
public $db;
public function __construct () {
private $db;
public $data;
public $isNew = true;
public $returnType = 'Object';
public function __construct ($data = null) {
$this->db = MysqliDb::getInstance();
if ($data)
$this->data = $data;
}
private function cleanup ($data) {
if (method_exists ($this, "preLoad"))
$this->preLoad ($data);
public function __set ($name, $value) {
$this->data[$name] = $value;
}
if (count ($data) == 0)
return Array();
public function __get ($name) {
if (isset ($this->data[$name]))
return $this->data[$name];
foreach ($this->dbFields as $key => $prop) {
$value = $data[$key];
if (!is_array($value)) {
$sqlData[$key] = $value;
continue;
}
if (property_exists ($this->db, $name))
return $this->db->$name;
}
if ($prop['json'])
$sqlData[$key] = json_encode($value);
else if ($prop['array'])
$sqlData[$key] = implode ("|", $value);
else
$sqlData[$key] = $value;
}
public function __isset ($name) {
if ($this->data[$name])
return isset ($this->data[$name]);
return $sqlData;
if (property_exists ($this->db, $name))
return isset ($this->db->$name);
}
public function insert (&$data) {
if (empty ($this->dbFields))
return false;
public function __unset ($name) {
unset ($this->data[$name]);
}
$data = $this->cleanup ($data);
$id = $this->db->insert ($this->dbTable, $data);
$data[$this->$primaryKey] = $id;
return $id;
public static function ObjectBuilder () {
$obj = self::objectCopy ();
$obj->returnType = 'Object';
return $obj;
}
public function remove ($data) {
$this->db->where ($this->primaryKey, $data['id']);
return $this->db->delete ($this->dbTable);
public static function ArrayBuilder () {
$obj = self::objectCopy ();
$obj->returnType = 'Array';
return $obj;
}
public function update ($data) {
public function insert () {
$sqlData = $this->prepareData ();
$id = $this->db->insert ($this->dbTable, $sqlData);
if (!empty ($this->primaryKey))
$this->data[$this->primaryKey] = $id;
$this->isNew = false;
return $id;
}
public function update ($data = null) {
if (empty ($this->dbFields))
return false;
$data = $this->cleanup ($data);
$this->db->where ($this->primaryKey, $data[$this->primaryKey]);
return $this->db->update ($this->dbTable, $data);
if (empty ($this->data[$this->primaryKey]))
return false;
if ($data) {
foreach ($data as $k => $v)
$this->$k = $v;
}
$sqlData = $this->prepareData ();
$this->db->where ($this->primaryKey, $this->data[$this->primaryKey]);
return $this->db->update ($this->dbTable, $sqlData);
}
public function getOne($id, $fields = null) {
public function save () {
if ($this->isNew)
return $this->insert();
return $this->update();
}
public function remove () {
$this->db->where ($this->primaryKey, $this->data[$this->primaryKey]);
return $this->db->delete ($this->dbTable);
}
public function byId ($id, $fields = null) {
$this->db->where($this->primaryKey, $id);
return $this->getOne ($fields);
}
public function getOne ($fields = null) {
$results = $this->db->getOne ($this->dbTable, $fields);
if (isset($this->jsonFields) && is_array($this->jsonFields)) {
foreach ($this->jsonFields as $key)
......@@ -66,12 +102,18 @@ abstract class dbObject {
foreach ($this->arrayFields as $key)
$results[$key] = explode ("|", $results[$key]);
}
return $results;
if ($this->returnType == 'Array')
return $results;
$item = $this->objectCopy ($results);
$item->isNew = false;
return $item;
}
public function get ($limit = null, $fields = null) {
$db = MysqliDb::getInstance();
$results = $db->get($this->dbTable);
$objects = Array ();
$results = $this->db->get($this->dbTable);
foreach ($results as &$r) {
if (isset ($this->jsonFields) && is_array($this->jsonFields)) {
foreach ($this->jsonFields as $key)
......@@ -81,28 +123,68 @@ abstract class dbObject {
foreach ($this->arrayFields as $key)
$r[$key] = explode ("|", $r[$key]);
}
if ($this->returnType == 'Object') {
$item = $this->objectCopy ($r);
$item->isNew = false;
$objects[] = $item;
}
}
if ($this->returnType == 'Object')
return $objects;
return $results;
}
public function where ($whereProp, $whereValue = null, $operator = null) {
$this->db->where ($whereProp, $whereValue, $operator);
return $this;
public function count () {
$res = $this->db->getValue ($this->dbTable, "count(*)");
return $res['cnt'];
}
public function orWhere ($whereProp, $whereValue = null, $operator = null) {
$this->db->orWhere ($whereProp, $whereValue, $operator);
public function __call ($method, $arg) {
call_user_func_array (array ($this->db, $method), $arg);
return $this;
}
public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields = null) {
$this->db->orderBy ($orderByField, $orderbyDirection, $customFields);
return $this;
public function toJson () {
return json_encode ($this->data);
}
public function count () {
$res = $this->db->getValue($this->dbTable, "count(*)");
return $res['cnt'];
public function __toString () {
return $this->toJson ();
}
private function prepareData () {
$sqlData = Array();
if (method_exists ($this, "preLoad"))
$this->preLoad ($data);
if (count ($this->data) == 0)
return Array();
foreach ($this->data as $key => $value) {
if (!in_array ($key, array_keys ($this->dbFields)))
continue;
if (!is_array($value)) {
$sqlData[$key] = $value;
continue;
}
if (in_array ($key, $this->jsonFields))
$sqlData[$key] = json_encode($value);
else
$sqlData[$key] = implode ("|", $value);
}
return $sqlData;
}
private static function objectCopy ($data = null) {
$className = get_called_class ();
return new $className ($data);
}
}
?>
......@@ -5,8 +5,13 @@ class accData extends dbObject {
protected $dbTable = "acc_data";
protected $primaryKey = "id";
public $calldate;
public $callid;
public $clientid;
public $queueid;
public function last () {
$this->db->where ("id" , 1300, '>');
$this->where ("id" , 1300, '>');
return $this;
}
}
......
<?php
require_once "../dbObject.php";
/**
* To make IDEs autocomplete happy
*
* @property string id
* @property string userid
* @property string name
* @property string authcode
* @property string iscallerid
*/
class department extends dbObject {
protected $dbTable = "departments";
protected $primaryKey = "id";
protected $dbFields = Array (
'userid' => 'int:required',
'name' => 'int:required',
'authcode' => 'int',
'iscallerid' => 'int',
'testvar' => 'int'
);
protected $jsonFields = Array ('authcode');
public function last () {
$this->setTrace (true);
$this->where ("id" , 130, '>');
return $this;
}
}
?>
<?
require_once ("../MysqliDb.php");
require_once ("models/accData.php");
require_once ("models/department.php");
$db = new Mysqlidb('localhost', 'root', '', 'akorbi');
$accData = new accData();
$d = $accData->getOne(1288);
print_r ($d);
$dept = new department();
$dept->userid = 10;
$dept->name = 'avb test';
$dept->authcode = Array('1234','123456');
$dept->iscallerid = 1;
$dept->insert();
print_r ($accData->last()->get());
$dept2 = new department([
'userid' => '11',
'name' => 'avb2 test',
'authcode' => '5678',
'iscallerid' => 0,
]);
$dept2->save();
$dept2->iscallerid=1;
print_r ($dept2->data);
$dept2->save();
//$a = new accData;
//echo $db->getLastQuery();
echo "List\n";
$depts = department::ObjectBuilder()->last()->get ();
foreach ($depts as $d) {
// print_r ($d->data);
echo $d . "\n";
}
echo "getOne\n";
$dept3 = department::ObjectBuilder()->byId ("181");
$dept3->authcode=333;
$dept3->save();
print_r ($dept3->data) . "\n";
echo $dept3->count;
print_r ($dept3->trace);
echo $dept3->qqq;
?>
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