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()
25 if (self::$dbo !== null)
27 self::$config = @include(
"core/config.inc.php");
28 if (empty(self::$config))
30 self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
36 return self::$config[3];
41 $className = new \ReflectionClass($this);
42 return $this->getDbPrefix().strtolower($className->getShortName());
47 $tables = array(
"Admin",
"User",
"Address",
"Cart",
"Category",
"Product",
"CartProduct",
"Meta",
"Cms",
"Config",
"Module",
"ModuleHook");
50 self::$dbo->beginTransaction();
53 foreach ($tables as $i)
57 if ($table->install() !=
true)
63 echo $e->getMessage();
64 self::$dbo->rollBack();
73 $fetcher = new \Entity\Meta();
74 return $fetcher->query(array(
"type" => get_class()));
80 return $this->getMeta();
83 if (!isset($this->fieldsValues[$key]))
85 return $this->fieldsValues[$key];
88 public function __set($key, $value)
90 if ($value instanceof \DateTime)
91 $value = $value->format(
"Y-m-d");
92 $this->fieldsValues[$key] = $value;
94 $this->changed[$key] = $value ? 1 : 0;
96 $this->changed[$key] = self::$dbo->quote($value);
104 if ($this->
id === null)
106 if (empty ($this->changed))
108 $query =
"INSERT INTO `{$this->getTableName()}` () VALUES ()";
109 $result = self::$dbo->exec($query);
111 throw new \Exception(self::$dbo->errorInfo()[2]);
115 $query =
"INSERT INTO `{$this->getTableName()}` (`" .implode(
"`,`", array_keys($this->changed)) .
"`) VALUES (" . implode(
",", $this->changed) .
")";
116 $result = self::$dbo->exec($query);
118 throw new \Exception(self::$dbo->errorInfo()[2]);
119 $this->changed = array();
121 $this->
id = self::$dbo->lastInsertId();
125 if (!empty($this->changed))
127 if ($this->
id ===
false)
128 throw new \Exception(
"Cannot update private row");
129 $query =
"UPDATE {$this->getTableName()} SET ";
130 $newValues = array();
131 foreach ($this->changed as $i => $j)
132 $newValues[] =
"`{$i}`=" . self::$dbo->quote($j);
133 $query .= implode(
",",$newValues).
" WHERE id={$this->id}";
134 $result = self::$dbo->exec($query);
136 throw new \Exception(self::$dbo->errorInfo()[2]);
137 $this->changed = array();
144 public function selects($criteria = null, $orderBy = null)
146 $query =
"SELECT * FROM {$this->getTableName()}";
148 if (!empty($criteria))
151 foreach ($criteria as $i => $j)
154 $subQuery[] =
"`{$i}` IS NULL";
155 else if (is_array($j))
159 $inArray[] = self::$dbo->quote($k);
160 $subQuery[] =
"`{$i}` IN (".implode(
",", $inArray).
")";
163 $subQuery[] =
"`{$i}`=".self::$dbo->quote($j);
165 $query .=
" WHERE ".implode(
" AND ", $subQuery);
167 if (!empty($orderBy))
170 foreach ($orderBy as $i => $j)
173 $_orderBy[] =
"`{$j}` ASC";
177 if (strtoupper($j ==
"DESC"))
179 $_orderBy[] =
"`{$i}` {$orderType}";
182 $query .=
" ORDER BY ".implode(
",", $_orderBy);
184 $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
185 if ($result ===
false)
186 throw new \Exception(self::$dbo->errorInfo()[2]);
187 $resultObj = array();
188 $className = get_class($this);
189 foreach ($result as $i)
191 $iObj =
new $className();
193 $resultObj[] = $iObj;
200 $query =
"SELECT * FROM {$this->getTableName()} WHERE id=".(int)$id.
" LIMIT 1";
201 $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
202 if ($result ===
false)
203 throw new \Exception(
"Cannot fetch data: ".self::$dbo->errorInfo()[2]);
204 $this->populate($result);
210 foreach ($data as $i => $j)
211 $this->fieldsValues[$i] = $j;
212 if (isset($data[
"id"]))
213 $this->
id = (int) $data[
"id"];
selects($criteria=null, $orderBy=null)