123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Created by PhpStorm.
- * User: root
- * Date: 31.07.18
- * Time: 10:49
- */
- require_once "DB.php";
- require_once "Comment.php";
- require_once "User.php";
- require_once "Post.php";
- abstract class Model
- {
- public static $objects = [];
- public $tableName;
- public $pkName;
- public $connection;
- public $entry;
- protected $dirty;
- protected $objDescTable;
- public static $descTable;
- public function __construct(array $entry = [])
- {
- $this->tableName = strtolower(get_called_class()) . "s";
- $this->pkName = static::getPkName();
- $this->connection = DB::getConnection();
- $this->entry = $entry;
- $this->dirty = false;
- $this->objDescTable = self::descTable();
- }
- public static function getByPk(int $pk)
- {
- if (isset(self::$objects[get_called_class() . "." . $pk])) return self::$objects[get_called_class() . "." . $pk];
- $query =
- "SELECT * FROM " . strtolower(get_called_class()) . "s" .
- " WHERE " . static::getPkName() . " = " . $pk;
- $connect = DB::getConnection();
- $result = $connect->query($query)->fetchAll(PDO::FETCH_ASSOC);
- return new static($result[0]);
- }
- protected static function descTable(): array
- {
- $query = "DESC " . strtolower(get_called_class()) . "s";
- $connect = DB::getConnection();
- $result = $connect->query($query)->fetchAll(PDO::FETCH_ASSOC);
- return $result;
- }
- protected static function getPkName(): string
- {
- foreach (static::$descTable ?? static::descTable() as $value) {
- if ($value["Key"] === "PRI") return $value["Field"];
- }
- }
- public function __get($name)
- {
- foreach ($this->entry as $key => $value) {
- if ($key === $name) return $value;
- }
- }
- public function __set($name, $setValue)
- {
- foreach ($this->objDescTable as $value) {
- if (strtolower($value["Field"]) === $name) {
- $oldValue = $this->entry[$name] ?? "";
- $this->entry[$name] = $setValue;
- if ($this->entry[$name] !== $oldValue) $this->dirty = true;
- }
- }
- }
- protected function getPreparedArguments()
- {
- $preparedRequest = [];
- foreach ($this->entry as $key => $value) {
- if ($key === $this->pkName) continue;
- $preparedRequest[] = $key . " = :" . $key;
- }
- return " SET " . implode(", ", $preparedRequest) . " ";
- }
- protected function getPreparedEntry()
- {
- $array = [];
- foreach ($this->entry as $key => $value) {
- if ($key === $this->pkName) continue;
- $array[$key] = $value;
- }
- return $array;
- }
- public function save()
- {
- $identityNewEntry = $this->entry[$this->pkName] ?? false;
- $operation = $identityNewEntry ? "UPDATE " : "INSERT INTO ";
- $condition = $operation === "UPDATE " ? " WHERE {$this->pkName} = {$this->entry["id"]}" : "";
- $query = $operation . $this->tableName . $this->getPreparedArguments() . $condition;
- $sth = $this->connection->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
- $sth->execute($this->getPreparedEntry());
- if (!$identityNewEntry) $this->entry[$this->pkName] = $this->connection->lastInsertId();
- Model::$objects[get_called_class() . "." . $this->entry[$this->pkName]] = $this;
- $this->dirty = false;
- }
- }
|