ModelBase.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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", "ModuleHook");
  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. \Tools\Hook::trigger("onBeforeEntitySave");
  88. \Tools\Hook::trigger("onBeforeEntitySave".$this->getTableName());
  89. if ($this->id === null)
  90. {
  91. if (empty ($this->changed))
  92. {
  93. //TODO
  94. return true;
  95. }
  96. else
  97. {
  98. $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
  99. $result = self::$dbo->exec($query);
  100. if (!$result)
  101. throw new \Exception(self::$dbo->errorInfo()[2]);
  102. $this->changed = array();
  103. }
  104. //TODO get last id
  105. }
  106. else
  107. {
  108. if (!empty($this->changed))
  109. {
  110. if ($this->id === false)
  111. throw new \Exception("Cannot update private row");
  112. $query = "UPDATE {$this->getTableName()} SET ";
  113. $newValues = array();
  114. foreach ($this->changed as $i => $j)
  115. $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
  116. $query .= implode(",",$newValues)." WHERE id={$this->id}";
  117. $result = self::$dbo->exec($query);
  118. if (!$result)
  119. throw new \Exception(self::$dbo->errorInfo()[2]);
  120. $this->changed = array();
  121. }
  122. }
  123. \Tools\Hook::trigger("onAfterEntitySave");
  124. \Tools\Hook::trigger("onAfterEntitySave".$this->getTableName());
  125. }
  126. public function selects($criteria = null)
  127. {
  128. $query = "SELECT * FROM {$this->getTableName()}";
  129. if (!empty($criteria))
  130. {
  131. $subQuery = array();
  132. foreach ($criteria as $i => $j)
  133. {
  134. if ($j == null)
  135. $subQuery[] = "`{$i}` IS NULL";
  136. else
  137. $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
  138. }
  139. $query .= " WHERE ".implode(" AND ", $subQuery);
  140. }
  141. $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
  142. if ($result === false)
  143. throw new \Exception(self::$dbo->errorInfo()[2]);
  144. $resultObj = array();
  145. $className = get_class($this);
  146. foreach ($result as $i)
  147. {
  148. $iObj = new $className($this);
  149. $iObj->populate($i);
  150. $resultObj[] = $iObj;
  151. }
  152. return $resultObj;
  153. }
  154. //TODO@1
  155. public function selectById($id)
  156. {
  157. }
  158. private function populate($data)
  159. {
  160. $this->id = FALSE;
  161. foreach ($data as $i => $j)
  162. $this->fieldsValues[$i] = $j;
  163. if (isset($data["id"]))
  164. $this->id = (int) $data["id"];
  165. }
  166. }