7 private $fieldsValues = array();
8 protected static $dbo = null;
9 private $changed = array();
10 private static $config = null;
13 protected abstract function install();
19 if ($id !== null && is_numeric($id))
20 $this->selectById($id);
23 public static function init($config = null)
25 if (self::$dbo !== null)
29 if ($config === null && self::$config === null)
31 self::$config = @include(
"core/config.inc.php");
32 if (empty(self::$config))
37 self::$config = $config;
38 self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
44 return self::$config[3];
49 $className = new \ReflectionClass($this);
50 return $this->getDbPrefix().strtolower($className->getShortName());
55 $tables = array(
"Admin",
"User",
"Address",
"Cart",
"Category",
"Product",
"CartProduct",
"Meta",
"Cms",
"Config",
"Module",
"ModuleHook");
58 self::$dbo->beginTransaction();
61 foreach ($tables as $i)
65 if ($table->install() !=
true)
66 throw new \
Exception(
"{$i}: Cannot table setup failure");
71 self::$dbo->rollBack();
72 error_log($e->getMessage());
81 $fetcher = new \Entity\Meta();
82 return $fetcher->query(array(
"type" => get_class($this)));
88 return $this->getMeta();
90 return $this->
id === null ? null : (int) $this->
id;
91 if (!isset($this->fieldsValues[$key]))
93 return $this->fieldsValues[$key];
96 public function __set($key, $value)
98 if ($value instanceof \DateTime)
99 $value = $value->format(
"Y-m-d");
100 $this->fieldsValues[$key] = $value;
102 $this->changed[$key] = $value ? 1 : 0;
103 else if ($value === null)
104 $this->changed[$key] =
"NULL";
106 $this->changed[$key] = self::$dbo->quote($value);
114 if ($this->
id === null)
116 if (empty ($this->changed))
118 $query =
"INSERT INTO `{$this->getTableName()}` () VALUES ()";
119 $result = self::$dbo->exec($query);
121 throw new \Exception(self::$dbo->errorInfo()[2]);
125 $query =
"INSERT INTO `{$this->getTableName()}` (`" .implode(
"`,`", array_keys($this->changed)) .
"`) VALUES (" . implode(
",", $this->changed) .
")";
126 $result = self::$dbo->exec($query);
128 throw new \Exception(self::$dbo->errorInfo()[2]);
129 $this->changed = array();
131 $this->
id = self::$dbo->lastInsertId();
135 if (!empty($this->changed))
137 if ($this->
id ===
false)
138 throw new \Exception(
"Cannot update private row");
139 $query =
"UPDATE {$this->getTableName()} SET ";
140 $newValues = array();
141 foreach ($this->changed as $i => $j)
142 $newValues[] =
"`{$i}`=" . $j;
143 $query .= implode(
",",$newValues).
" WHERE id={$this->id}";
144 $result = self::$dbo->exec($query);
146 throw new \Exception(self::$dbo->errorInfo()[2]);
147 $this->changed = array();
159 public function delete()
161 if ($this->
id ===
false)
165 $id = (int) $this->
id;
166 foreach ($this->fieldsValues as $i => $j)
167 if (!isset($this->changed[$i]))
168 $this->changed[$i] = $j;
169 if (self::$dbo->exec(
"DELETE FROM {$this->getTableName()} WHERE `id`={$id}") ===
false)
176 public function selects($criteria = null, $orderBy = null)
178 $query =
"SELECT * FROM {$this->getTableName()}";
180 if (!empty($criteria))
183 foreach ($criteria as $i => $j)
186 $subQuery[] =
"`{$i}` IS NULL";
187 else if (is_array($j))
191 $inArray[] = self::$dbo->quote($k);
192 $subQuery[] =
"`{$i}` IN (".implode(
",", $inArray).
")";
195 $subQuery[] =
"`{$i}`=".self::$dbo->quote($j);
197 $query .=
" WHERE ".implode(
" AND ", $subQuery);
199 if (!empty($orderBy))
202 foreach ($orderBy as $i => $j)
205 $_orderBy[] =
"`{$j}` ASC";
209 if (strtoupper($j ==
"DESC"))
211 $_orderBy[] =
"`{$i}` {$orderType}";
214 $query .=
" ORDER BY ".implode(
",", $_orderBy);
216 $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
217 if ($result ===
false)
218 throw new \Exception(self::$dbo->errorInfo()[2]);
219 $resultObj = array();
220 $className = get_class($this);
221 foreach ($result as $i)
223 $iObj =
new $className();
225 $resultObj[] = $iObj;
232 $query =
"SELECT * FROM {$this->getTableName()} WHERE id=".(int)$id.
" LIMIT 1";
233 $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
234 if ($result ===
false || empty($result))
235 throw new \Exception(
"Cannot fetch data: ".self::$dbo->errorInfo()[2]);
236 $this->populate($result->fetch());
243 throw new \Exception(
"Cannot load entity: no result found");
244 foreach ($data as $i => $j)
245 $this->fieldsValues[$i] = $j;
246 if (isset($this->fieldsValues[
"id"]))
247 $this->
id = (int) $this->fieldsValues[
"id"];
248 $this->changed = array();
static init($config=null)
selects($criteria=null, $orderBy=null)