Definition at line 5 of file ModelBase.php.
| __construct |
( |
|
$id = null | ) |
|
Definition at line 85 of file ModelBase.php.
90 return $this->
id === null ? null : (int) $this->
id;
91 if (!isset($this->fieldsValues[$key]))
93 return $this->fieldsValues[$key];
Definition at line 96 of file ModelBase.php.
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);
Definition at line 79 of file ModelBase.php.
81 $fetcher = new \Entity\Meta();
82 return $fetcher->query(array(
"type" => get_class($this)));
Definition at line 47 of file ModelBase.php.
49 $className = new \ReflectionClass($this);
50 return $this->
getDbPrefix().strtolower($className->getShortName());
| static init |
( |
|
$config = null | ) |
|
|
static |
Definition at line 23 of file ModelBase.php.
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))
38 self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
Definition at line 215 of file ModelBase.php.
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();
Definition at line 108 of file ModelBase.php.
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();
Definition at line 206 of file ModelBase.php.
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]);
| selects |
( |
|
$criteria = null, |
|
|
|
$orderBy = null |
|
) |
| |
Definition at line 152 of file ModelBase.php.
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;
Definition at line 53 of file ModelBase.php.
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());
The documentation for this class was generated from the following file: