id = null; if ($id !== null) $this->selectById($id); } public static function init() { if (self::$dbo !== null) return true; self::$config = @include("core/config.inc.php"); if (empty(self::$config)) return false; self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]); return true; } public static function getDbPrefix() { return self::$config[3]; } public function getTableName() { $className = new \ReflectionClass($this); return $this->getDbPrefix().strtolower($className->getShortName()); } public static function setup() { $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module", "ModuleHook"); self::init(); self::$dbo->beginTransaction(); try { foreach ($tables as $i) { $i = "Entity\\".$i; $table = new $i(); if ($table->install() != true) throw new \Exception(); } } catch (\Exception $e) { echo $e->getMessage(); self::$dbo->rollBack(); return false; } self::$dbo->commit(); return true; } public function getMeta($lang=null) { $fetcher = new \Entity\Meta(); return $fetcher->query(array("type" => get_class())); } public function __get($key) { if ($key == "meta") return $this->getMeta(); if (!isset($this->fieldsValues[$key])) return null; return $this->fieldsValues[$key]; } public function __set($key, $value) { if ($value instanceof \DateTime) $value = $value->format("Y-m-d"); $this->fieldsValues[$key] = $value; if (is_bool($value)) $this->changed[$key] = $value ? 1 : 0; else $this->changed[$key] = self::$dbo->quote($value); return $value; } public function save() { //TODO send pre-hook if ($this->id === null) { if (empty ($this->changed)) { //TODO return true; } else { $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")"; $result = self::$dbo->exec($query); if (!$result) throw new \Exception(self::$dbo->errorInfo()[2]); $this->changed = array(); } //TODO get last id } else { if (empty($this->changed)) return true; if ($this->id === false) throw new \Exception("Cannot update private row"); $query = "UPDATE {$this->getTableName()} SET "; $newValues = array(); foreach ($this->changed as $i => $j) $newValues[] = "`{$i}`=" . self::$dbo->quote($j); $query .= implode(",",$newValues)." WHERE id={$this->id}"; $result = self::$dbo->exec($query); if (!$result) throw new \Exception(self::$dbo->errorInfo()[2]); $this->changed = array(); } //TODO send hook } public function selects($criteria = null) { $query = "SELECT * FROM {$this->getTableName()}"; if (!empty($criteria)) { $subQuery = array(); foreach ($criteria as $i => $j) { if ($j == null) $subQuery[] = "`{$i}` IS NULL"; else $subQuery[] = "`{$i}`=".self::$dbo->quote($j); } $query .= " WHERE ".implode(" AND ", $subQuery); } $result = self::$dbo->query($query, \PDO::FETCH_ASSOC); if ($result === false) throw new \Exception(self::$dbo->errorInfo()[2]); $resultObj = array(); $className = get_class($this); foreach ($result as $i) { $iObj = new $className($this); $iObj->populate($i); $resultObj[] = $iObj; } return $resultObj; } //TODO public function selectById($id) { } private function populate($data) { $this->id = FALSE; foreach ($data as $i => $j) $this->fieldsValues[$i] = $j; if (isset($data["id"])) $this->id = (int) $data["id"]; } }