ModelBase.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 function getTableName()
  33. {
  34. $className = new \ReflectionClass($this);
  35. return $this->getDbPrefix().strtolower($className->getShortName());
  36. }
  37. public static function setup()
  38. {
  39. $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module");
  40. self::init();
  41. self::$dbo->beginTransaction();
  42. try
  43. {
  44. foreach ($tables as $i)
  45. {
  46. $i = "Entity\\".$i;
  47. $table = new $i();
  48. if ($table->install() != true)
  49. throw new \Exception();
  50. }
  51. }
  52. catch (\Exception $e)
  53. {
  54. echo $e->getMessage();
  55. self::$dbo->rollBack();
  56. return false;
  57. }
  58. self::$dbo->commit();
  59. return true;
  60. }
  61. public function getMeta($lang=null)
  62. {
  63. $fetcher = new \Entity\Meta();
  64. return $fetcher->query(array("type" => get_class()));
  65. }
  66. public function __get($key)
  67. {
  68. if ($key == "meta")
  69. return $this->getMeta();
  70. if (!isset($this->fieldsValues[$key]))
  71. return null;
  72. return $this->fieldsValues[$key];
  73. }
  74. public function __set($key, $value)
  75. {
  76. if ($value instanceof \DateTime)
  77. $value = $value->format("Y-m-d");
  78. $this->fieldsValues[$key] = $value;
  79. if (is_bool($value))
  80. $this->changed[$key] = $value ? 1 : 0;
  81. else
  82. $this->changed[$key] = self::$dbo->quote($value);
  83. return $value;
  84. }
  85. public function save()
  86. {
  87. //TODO send pre-hook
  88. if ($this->id === null)
  89. {
  90. if (empty ($this->changed))
  91. {
  92. //TODO
  93. return true;
  94. }
  95. else
  96. {
  97. $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
  98. $result = self::$dbo->exec($query);
  99. if (!$result)
  100. throw new \Exception(self::$dbo->errorInfo()[2]);
  101. $this->changed = array();
  102. }
  103. //TODO get last id
  104. }
  105. else
  106. {
  107. if (empty($this->changed))
  108. return true;
  109. if ($this->id === false)
  110. throw new \Exception("Cannot update private row");
  111. $query = "UPDATE {$this->getTableName()} SET ";
  112. $newValues = array();
  113. foreach ($this->changed as $i => $j)
  114. $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
  115. $query .= implode(",",$newValues)." WHERE id={$this->id}";
  116. $result = self::$dbo->exec($query);
  117. if (!$result)
  118. throw new \Exception(self::$dbo->errorInfo()[2]);
  119. $this->changed = array();
  120. }
  121. //TODO send hook
  122. }
  123. public function selects($criteria = null)
  124. {
  125. $query = "SELECT * FROM {$this->getTableName()}";
  126. if (!empty($criteria))
  127. {
  128. $subQuery = array();
  129. foreach ($criteria as $i => $j)
  130. {
  131. if ($j == null)
  132. $subQuery[] = "`{$i}` IS NULL";
  133. else
  134. $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
  135. }
  136. $query .= " WHERE ".implode(" AND ", $subQuery);
  137. }
  138. $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
  139. if ($result === false)
  140. throw new \Exception(self::$dbo->errorInfo()[2]);
  141. $resultObj = array();
  142. $className = get_class($this);
  143. foreach ($result as $i)
  144. {
  145. $iObj = new $className($this);
  146. $iObj->populate($i);
  147. $resultObj[] = $iObj;
  148. }
  149. return $resultObj;
  150. }
  151. //TODO
  152. public function selectById($id)
  153. {
  154. }
  155. private function populate($data)
  156. {
  157. $this->id = FALSE;
  158. foreach ($data as $i => $j)
  159. $this->fieldsValues[$i] = $j;
  160. if (isset($data["id"]))
  161. $this->id = (int) $data["id"];
  162. }
  163. }