ModelBase.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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");
  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. $this->changed[$key] = self::$dbo->quote($value);
  80. return $value;
  81. }
  82. public function save()
  83. {
  84. //TODO send pre-hook
  85. if ($this->id === null)
  86. {
  87. if (empty ($this->changed))
  88. {
  89. //TODO
  90. return true;
  91. }
  92. else
  93. {
  94. $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
  95. $statement = self::$dbo->prepare($query);
  96. $result = $statement->execute();
  97. if (!$result)
  98. throw new \Exception($statement->errorInfo()[2]);
  99. $this->changed = array();
  100. }
  101. //TODO get last id
  102. }
  103. else
  104. {
  105. if ($this->id === false)
  106. throw new \Exception("Cannot update private row");
  107. }
  108. //TODO send hook
  109. }
  110. public function selects($criteria = null)
  111. {
  112. $query = "SELECT * FROM {$this->getTableName()}";
  113. if (!empty($criteria))
  114. {
  115. $subQuery = array();
  116. foreach ($criteria as $i => $j)
  117. {
  118. if ($j == null)
  119. $subQuery[] = "`{$i}` IS NULL";
  120. else
  121. $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
  122. }
  123. $query .= " WHERE ".implode(" AND ", $subQuery);
  124. }
  125. $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
  126. $resultObj = array();
  127. $className = get_class($this);
  128. foreach ($result as $i)
  129. {
  130. $iObj = new $className($this);
  131. $iObj->populate($i);
  132. $resultObj[] = $iObj;
  133. }
  134. return $resultObj;
  135. }
  136. //TODO
  137. public function selectById($id)
  138. {
  139. }
  140. private function populate($data)
  141. {
  142. $this->id = FALSE;
  143. foreach ($data as $i => $j)
  144. $this->fieldsValues[$i] = $j;
  145. }
  146. }