ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Attributes | Private Member Functions | Private Attributes | Static Private Attributes
ModelBase Class Reference
Inheritance diagram for ModelBase:
Address Admin Cart CartProduct Category Cms Config Meta Module ModuleHook Product User

Public Member Functions

 __construct ($id=null)
 
 getTableName ()
 
 getMeta ($lang=null)
 
 __get ($key)
 
 __set ($key, $value)
 
 save ()
 
 delete ()
 
 selects ($criteria=null, $orderBy=null)
 
 selectById ($id)
 

Static Public Member Functions

static init ($config=null)
 
static getDbPrefix ()
 
static setup ()
 

Protected Member Functions

 install ()
 

Static Protected Attributes

static $dbo = null
 

Private Member Functions

 populate ($data)
 

Private Attributes

 $fieldsValues = array()
 
 $changed = array()
 
 $id
 

Static Private Attributes

static $config = null
 

Detailed Description

Definition at line 5 of file ModelBase.php.

Constructor & Destructor Documentation

__construct (   $id = null)

Definition at line 15 of file ModelBase.php.

16  {
17  self::init();
18  $this->id = null;
19  if ($id !== null && is_numeric($id))
20  $this->selectById($id);
21  }

Member Function Documentation

__get (   $key)

Definition at line 85 of file ModelBase.php.

86  {
87  if ($key == "meta")
88  return $this->getMeta();
89  if ($key == "id")
90  return $this->id === null ? null : (int) $this->id;
91  if (!isset($this->fieldsValues[$key]))
92  return null;
93  return $this->fieldsValues[$key];
94  }
getMeta($lang=null)
Definition: ModelBase.php:79
__set (   $key,
  $value 
)

Definition at line 96 of file ModelBase.php.

97  {
98  if ($value instanceof \DateTime)
99  $value = $value->format("Y-m-d");
100  $this->fieldsValues[$key] = $value;
101  if (is_bool($value))
102  $this->changed[$key] = $value ? 1 : 0;
103  else if ($value === null)
104  $this->changed[$key] = "NULL";
105  else
106  $this->changed[$key] = self::$dbo->quote($value);
107  return $value;
108  }
delete ( )

Remove entity from database $entity->delete() then $entity->save() should save entity with a new id and the same data

Returns
Boolean true on success

Definition at line 159 of file ModelBase.php.

160  {
161  if ($this->id === false)
162  return true;
163  \Tools\Hooks::trigger("onBeforeEntityDelete");
164  \Tools\Hooks::trigger("onBeforeEntityDelete".$this->getTableName());
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)
170  return false;
171  \Tools\Hooks::trigger("onAfterEntityDelete");
172  \Tools\Hooks::trigger("onAfterEntityDelete".$this->getTableName());
173  return true;
174  }
trigger($hookName, $params=null)
Definition: Hooks.php:70
static getDbPrefix ( )
static

Definition at line 42 of file ModelBase.php.

43  {
44  return self::$config[3];
45  }
getMeta (   $lang = null)

Definition at line 79 of file ModelBase.php.

80  {
81  $fetcher = new \Entity\Meta();
82  return $fetcher->query(array("type" => get_class($this)));
83  }
getTableName ( )

Definition at line 47 of file ModelBase.php.

48  {
49  $className = new \ReflectionClass($this);
50  return $this->getDbPrefix().strtolower($className->getShortName());
51  }
static getDbPrefix()
Definition: ModelBase.php:42
static init (   $config = null)
static

Definition at line 23 of file ModelBase.php.

24  {
25  if (self::$dbo !== null)
26  return true;
27  // @codeCoverageIgnoreStart
28  // Load config from written file; this file is created from setup script
29  if ($config === null && self::$config === null)
30  {
31  self::$config = @include("core/config.inc.php");
32  if (empty(self::$config))
33  return false;
34  }
35  // @codeCoverageIgnoreEnd
36  else
37  self::$config = $config;
38  self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
39  return true;
40  }
install ( )
abstractprotected
populate (   $data)
private

Definition at line 239 of file ModelBase.php.

240  {
241  $this->id = FALSE;
242  if ($data === false)
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();
249  }
save ( )

Definition at line 110 of file ModelBase.php.

111  {
112  \Tools\Hooks::trigger("onBeforeEntitySave");
113  \Tools\Hooks::trigger("onBeforeEntitySave".$this->getTableName());
114  if ($this->id === null)
115  {
116  if (empty ($this->changed))
117  {
118  $query = "INSERT INTO `{$this->getTableName()}` () VALUES ()";
119  $result = self::$dbo->exec($query);
120  if (!$result)
121  throw new \Exception(self::$dbo->errorInfo()[2]);
122  }
123  else
124  {
125  $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
126  $result = self::$dbo->exec($query);
127  if (!$result)
128  throw new \Exception(self::$dbo->errorInfo()[2]);
129  $this->changed = array();
130  }
131  $this->id = self::$dbo->lastInsertId();
132  }
133  else
134  {
135  if (!empty($this->changed))
136  {
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);
145  if (!$result)
146  throw new \Exception(self::$dbo->errorInfo()[2]);
147  $this->changed = array();
148  }
149  }
150  \Tools\Hooks::trigger("onAfterEntitySave");
151  \Tools\Hooks::trigger("onAfterEntitySave".$this->getTableName());
152  }
trigger($hookName, $params=null)
Definition: Hooks.php:70
selectById (   $id)

Definition at line 230 of file ModelBase.php.

231  {
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());
237  }
selects (   $criteria = null,
  $orderBy = null 
)

Definition at line 176 of file ModelBase.php.

177  {
178  $query = "SELECT * FROM {$this->getTableName()}";
179 
180  if (!empty($criteria))
181  {
182  $subQuery = array();
183  foreach ($criteria as $i => $j)
184  {
185  if ($j == null)
186  $subQuery[] = "`{$i}` IS NULL";
187  else if (is_array($j))
188  {
189  $inArray = [];
190  foreach ($j as $k)
191  $inArray[] = self::$dbo->quote($k);
192  $subQuery[] = "`{$i}` IN (".implode(",", $inArray).")";
193  }
194  else
195  $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
196  }
197  $query .= " WHERE ".implode(" AND ", $subQuery);
198  }
199  if (!empty($orderBy))
200  {
201  $_orderBy = array();
202  foreach ($orderBy as $i => $j)
203  {
204  if (is_numeric($i))
205  $_orderBy[] = "`{$j}` ASC";
206  else
207  {
208  $orderType = "ASC";
209  if (strtoupper($j == "DESC"))
210  $orderType = "DESC";
211  $_orderBy[] = "`{$i}` {$orderType}";
212  }
213  }
214  $query .= " ORDER BY ".implode(",", $_orderBy);
215  }
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)
222  {
223  $iObj = new $className();
224  $iObj->populate($i);
225  $resultObj[] = $iObj;
226  }
227  return $resultObj;
228  }
static setup ( )
static

Definition at line 53 of file ModelBase.php.

54  {
55  $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module", "ModuleHook");
56 
57  self::init();
58  self::$dbo->beginTransaction();
59  try
60  {
61  foreach ($tables as $i)
62  {
63  $i = "Entity\\".$i;
64  $table = new $i();
65  if ($table->install() != true)
66  throw new \Exception("{$i}: Cannot table setup failure");
67  }
68  }
69  catch (\Exception $e)
70  {
71  self::$dbo->rollBack();
72  error_log($e->getMessage());
73  return false;
74  }
75  self::$dbo->commit();
76  return true;
77  }

Field Documentation

$changed = array()
private

Definition at line 9 of file ModelBase.php.

$config = null
staticprivate

Definition at line 10 of file ModelBase.php.

$dbo = null
staticprotected

Definition at line 8 of file ModelBase.php.

$fieldsValues = array()
private

Definition at line 7 of file ModelBase.php.

$id
private

Definition at line 11 of file ModelBase.php.


The documentation for this class was generated from the following file: