Model.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 31.07.18
  6. * Time: 10:49
  7. */
  8. require_once "DB.php";
  9. require_once "Comment.php";
  10. require_once "User.php";
  11. require_once "Post.php";
  12. abstract class Model
  13. {
  14. public static $objects = [];
  15. public $tableName;
  16. public $pkName;
  17. public $connection;
  18. public $entry;
  19. protected $dirty;
  20. protected $objDescTable;
  21. public static $descTable;
  22. public function __construct(array $entry = [])
  23. {
  24. $this->tableName = strtolower(get_called_class()) . "s";
  25. $this->pkName = static::getPkName();
  26. $this->connection = DB::getConnection();
  27. $this->entry = $entry;
  28. $this->dirty = false;
  29. $this->objDescTable = self::descTable();
  30. }
  31. public static function getByPk(int $pk)
  32. {
  33. if (isset(self::$objects[get_called_class() . "." . $pk])) return self::$objects[get_called_class() . "." . $pk];
  34. $query =
  35. "SELECT * FROM " . strtolower(get_called_class()) . "s" .
  36. " WHERE " . static::getPkName() . " = " . $pk;
  37. $connect = DB::getConnection();
  38. $result = $connect->query($query)->fetchAll(PDO::FETCH_ASSOC);
  39. return new static($result[0]);
  40. }
  41. protected static function descTable(): array
  42. {
  43. $query = "DESC " . strtolower(get_called_class()) . "s";
  44. $connect = DB::getConnection();
  45. $result = $connect->query($query)->fetchAll(PDO::FETCH_ASSOC);
  46. return $result;
  47. }
  48. protected static function getPkName(): string
  49. {
  50. foreach (static::$descTable ?? static::descTable() as $value) {
  51. if ($value["Key"] === "PRI") return $value["Field"];
  52. }
  53. }
  54. public function __get($name)
  55. {
  56. foreach ($this->entry as $key => $value) {
  57. if ($key === $name) return $value;
  58. }
  59. }
  60. public function __set($name, $setValue)
  61. {
  62. foreach ($this->objDescTable as $value) {
  63. if (strtolower($value["Field"]) === $name) {
  64. $oldValue = $this->entry[$name] ?? "";
  65. $this->entry[$name] = $setValue;
  66. if ($this->entry[$name] !== $oldValue) $this->dirty = true;
  67. }
  68. }
  69. }
  70. protected function getPreparedArguments()
  71. {
  72. $preparedRequest = [];
  73. foreach ($this->entry as $key => $value) {
  74. if ($key === $this->pkName) continue;
  75. $preparedRequest[] = $key . " = :" . $key;
  76. }
  77. return " SET " . implode(", ", $preparedRequest) . " ";
  78. }
  79. protected function getPreparedEntry()
  80. {
  81. $array = [];
  82. foreach ($this->entry as $key => $value) {
  83. if ($key === $this->pkName) continue;
  84. $array[$key] = $value;
  85. }
  86. return $array;
  87. }
  88. public function save()
  89. {
  90. $identityNewEntry = $this->entry[$this->pkName] ?? false;
  91. $operation = $identityNewEntry ? "UPDATE " : "INSERT INTO ";
  92. $condition = $operation === "UPDATE " ? " WHERE {$this->pkName} = {$this->entry["id"]}" : "";
  93. $query = $operation . $this->tableName . $this->getPreparedArguments() . $condition;
  94. $sth = $this->connection->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  95. $sth->execute($this->getPreparedEntry());
  96. if (!$identityNewEntry) $this->entry[$this->pkName] = $this->connection->lastInsertId();
  97. Model::$objects[get_called_class() . "." . $this->entry[$this->pkName]] = $this;
  98. $this->dirty = false;
  99. }
  100. }