ModelBase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Entity;
  3. abstract class ModelBase
  4. {
  5. private $fieldsValues = array();
  6. protected static $dbo = null;
  7. private $changed = array();
  8. private static $config = null;
  9. private $id;
  10. protected abstract function install();
  11. public function __construct($id = null)
  12. {
  13. self::init();
  14. $this->id = null;
  15. if ($id !== null)
  16. $this->selectById($id);
  17. }
  18. public static function init()
  19. {
  20. if (self::$dbo !== null)
  21. return true;
  22. self::$config = @include("core/config.inc.php");
  23. if (empty(self::$config))
  24. return false;
  25. self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
  26. return true;
  27. }
  28. public static function getDbPrefix()
  29. {
  30. return self::$config[3];
  31. }
  32. public static function setup()
  33. {
  34. $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config");
  35. self::init();
  36. self::$dbo->beginTransaction();
  37. try
  38. {
  39. foreach ($tables as $i)
  40. {
  41. $i = "Entity\\".$i;
  42. $table = new $i();
  43. if ($table->install() != true)
  44. throw new \Exception();
  45. }
  46. }
  47. catch (\Exception $e)
  48. {
  49. echo $e->getMessage();
  50. self::$dbo->rollBack();
  51. return false;
  52. }
  53. self::$dbo->commit();
  54. return true;
  55. }
  56. public function getMeta($lang=null)
  57. {
  58. $fetcher = new \Entity\Meta();
  59. return $fetcher->query(array("type" => get_class()));
  60. }
  61. public function __get($key)
  62. {
  63. if ($key == "meta")
  64. return $this->getMeta();
  65. if (!isset($this->fieldsValues[$key]))
  66. return null;
  67. return $this->fieldsValues[$key];
  68. }
  69. public function __set($key, $value)
  70. {
  71. if ($value instanceof \DateTime)
  72. $value = $value->format("Y-m-d");
  73. $this->fieldsValues[$key] = $value;
  74. $this->changed[$key] = self::$dbo->quote($value);
  75. return $value;
  76. }
  77. public function save()
  78. {
  79. $className = new \ReflectionClass($this);
  80. $className = strtolower($className->getShortName());
  81. //TODO send pre-hook
  82. if ($this->id === null)
  83. {
  84. if (empty ($this->changed))
  85. {
  86. //TODO
  87. return true;
  88. }
  89. else
  90. {
  91. $query = "INSERT INTO `{$this->getDbPrefix()}{$className}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
  92. $statement = self::$dbo->prepare($query);
  93. $result = $statement->execute();
  94. if (!$result)
  95. throw new \Exception($statement->errorInfo()[2]);
  96. $this->changed = array();
  97. }
  98. //TODO get last id
  99. }
  100. else
  101. {
  102. if ($this->id === false)
  103. throw new \Exception("Cannot update private row");
  104. }
  105. //TODO send hook
  106. }
  107. //TODO
  108. public function selectById($id)
  109. {
  110. }
  111. private function populate($data)
  112. {
  113. $this->id = FALSE;
  114. }
  115. }