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;
104 $this->changed[$key] = self::$dbo->quote($value);
112 if ($this->
id === null)
114 if (empty ($this->changed))
116 $query =
"INSERT INTO `{$this->getTableName()}` () VALUES ()";
117 $result = self::$dbo->exec($query);
119 throw new \Exception(self::$dbo->errorInfo()[2]);
123 $query =
"INSERT INTO `{$this->getTableName()}` (`" .implode(
"`,`", array_keys($this->changed)) .
"`) VALUES (" . implode(
",", $this->changed) .
")";
124 $result = self::$dbo->exec($query);
126 throw new \Exception(self::$dbo->errorInfo()[2]);
127 $this->changed = array();
129 $this->
id = self::$dbo->lastInsertId();
133 if (!empty($this->changed))
135 if ($this->
id ===
false)
136 throw new \Exception(
"Cannot update private row");
137 $query =
"UPDATE {$this->getTableName()} SET ";
138 $newValues = array();
139 foreach ($this->changed as $i => $j)
140 $newValues[] =
"`{$i}`=" . $j;
141 $query .= implode(
",",$newValues).
" WHERE id={$this->id}";
142 $result = self::$dbo->exec($query);
144 throw new \Exception(self::$dbo->errorInfo()[2]);
145 $this->changed = array();
152 public function selects($criteria = null, $orderBy = null)
154 $query =
"SELECT * FROM {$this->getTableName()}";
156 if (!empty($criteria))
159 foreach ($criteria as $i => $j)
162 $subQuery[] =
"`{$i}` IS NULL";
163 else if (is_array($j))
167 $inArray[] = self::$dbo->quote($k);
168 $subQuery[] =
"`{$i}` IN (".implode(
",", $inArray).
")";
171 $subQuery[] =
"`{$i}`=".self::$dbo->quote($j);
173 $query .=
" WHERE ".implode(
" AND ", $subQuery);
175 if (!empty($orderBy))
178 foreach ($orderBy as $i => $j)
181 $_orderBy[] =
"`{$j}` ASC";
185 if (strtoupper($j ==
"DESC"))
187 $_orderBy[] =
"`{$i}` {$orderType}";
190 $query .=
" ORDER BY ".implode(
",", $_orderBy);
192 $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
193 if ($result ===
false)
194 throw new \Exception(self::$dbo->errorInfo()[2]);
195 $resultObj = array();
196 $className = get_class($this);
197 foreach ($result as $i)
199 $iObj =
new $className();
201 $resultObj[] = $iObj;
208 $query =
"SELECT * FROM {$this->getTableName()} WHERE id=".(int)$id.
" LIMIT 1";
209 $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
210 if ($result ===
false || empty($result))
211 throw new \Exception(
"Cannot fetch data: ".self::$dbo->errorInfo()[2]);
212 $this->populate($result->fetch());
218 foreach ($data as $i => $j)
219 $this->fieldsValues[$i] = $j;
220 if (isset($this->fieldsValues[
"id"]))
221 $this->
id = (int) $this->fieldsValues[
"id"];
222 $this->changed = array();
static init($config=null)
selects($criteria=null, $orderBy=null)