amdp3-metaforums/Application/Foundations/Model.php

119 lines
4.2 KiB
PHP
Raw Normal View History

2019-11-18 13:33:45 +00:00
<?php
namespace Application\Foundations;
use Application\Services\ServiceContainer;
2019-11-19 03:36:39 +00:00
class Model implements \JsonSerializable {
2019-11-18 13:33:45 +00:00
public $attributes;
protected $primary_key = 'id';
public function __construct() {
}
public function hydrate($data) {
$this->attributes = $data;
}
public static function create($data) {
$calling_class = get_called_class();
$class = explode('\\',get_called_class());
$tablename = strtolower($class[count($class)-1]);
$result = ServiceContainer::Database()->insert($tablename, $data);
if($result) {
$data['id'] = $result;
}
$inst = new $calling_class();
2019-11-19 03:36:39 +00:00
$inst->hydrate($data);
2019-11-18 13:33:45 +00:00
return $inst;
}
public static function find($key) {
$calling_class = get_called_class();
$inst = new $calling_class();
$class = explode('\\',get_called_class());
$tablename = strtolower($class[count($class)-1]);
$query = new QueryBuilder();
$query = $query->select('*')->from($tablename)->where($inst->primary_key,$key)->build();
$result = ServiceContainer::Database()->select($query);
if(count($result) == 0) return null;
$inst->hydrate($result[0]);
return $inst;
}
2019-11-19 03:36:39 +00:00
public static function select($where_query) {
$calling_class = get_called_class();
$class = explode('\\',get_called_class());
$tablename = strtolower($class[count($class)-1]);
$query = new QueryBuilder();
$query = $query->select('*')->from($tablename)->build();
$query .= $where_query->build();
$result = ServiceContainer::Database()->select($query);
if(count($result) == 0) return [];
foreach($result as $key => $val) {
$inst = new $calling_class();
$inst->hydrate($result[$key]);
$result[$key] = $inst;
}
return $result;
}
public static function selectOne($where_query) {
$calling_class = get_called_class();
$class = explode('\\',get_called_class());
$tablename = strtolower($class[count($class)-1]);
$query = new QueryBuilder();
$query = $query->select('*')->from($tablename)->build();
$query .= $where_query->build();
$result = ServiceContainer::Database()->select($query);
if(count($result) == 0) return null;
$inst = new $calling_class();
$inst->hydrate($result[0]);
return $inst;
}
public static function all() {
return self::select(new QueryBuilder());
}
2019-11-18 13:33:45 +00:00
public function update($key) {
$calling_class = get_called_class();
$class = explode('\\',get_called_class());
$tablename = strtolower($class[count($class)-1]);
$query = new QueryBuilder();
$query = $query->update($tablename)->set($key)->where($this->primary_key,$this->attributes[$this->primary_key])->build();
$result = ServiceContainer::Database()->update($query);
if(!$result) return null;
else {
return $this;
}
}
public function delete() {
$calling_class = get_called_class();
$class = explode('\\',get_called_class());
$tablename = strtolower($class[count($class)-1]);
$query = new QueryBuilder();
$query = $query->delete()->from($tablename)->where($this->primary_key,$this->attributes[$this->primary_key])->build();
$result = ServiceContainer::Database()->update($query);
if(!$result) return $this;
else {
return null;
}
}
function __get($prop) {
2019-11-19 03:36:39 +00:00
$methodName = $prop."_attribute";
if(method_exists($this,$methodName)) {
return $this->$methodName();
}
2019-11-18 13:33:45 +00:00
return $this->attributes[$prop];
}
function __set($prop, $val) {
$this->attributes[$prop] = $val;
}
2019-11-19 03:36:39 +00:00
public function jsonSerialize() {
$data = $this->attributes;
$attr = get_class_methods($this);
$attr = array_filter($attr, function ($var) {
return strpos($var, "_attribute") !== false;
});
foreach($attr as $attr) {
$attrName = substr($attr,0,strlen($attr) - strlen("_attribute"));
$data[$attrName] = $this->$attr();
}
return $data;
}
2019-11-18 13:33:45 +00:00
}