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 ()
 
 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  }
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 217 of file ModelBase.php.

218  {
219  $this->id = FALSE;
220  foreach ($data as $i => $j)
221  $this->fieldsValues[$i] = $j;
222  if (isset($this->fieldsValues["id"]))
223  $this->id = (int) $this->fieldsValues["id"];
224  $this->changed = array();
225  }
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:62
selectById (   $id)

Definition at line 208 of file ModelBase.php.

209  {
210  $query = "SELECT * FROM {$this->getTableName()} WHERE id=".(int)$id." LIMIT 1";
211  $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
212  if ($result === false || empty($result))
213  throw new \Exception("Cannot fetch data: ".self::$dbo->errorInfo()[2]);
214  $this->populate($result->fetch());
215  }
selects (   $criteria = null,
  $orderBy = null 
)

Definition at line 154 of file ModelBase.php.

155  {
156  $query = "SELECT * FROM {$this->getTableName()}";
157 
158  if (!empty($criteria))
159  {
160  $subQuery = array();
161  foreach ($criteria as $i => $j)
162  {
163  if ($j == null)
164  $subQuery[] = "`{$i}` IS NULL";
165  else if (is_array($j))
166  {
167  $inArray = [];
168  foreach ($j as $k)
169  $inArray[] = self::$dbo->quote($k);
170  $subQuery[] = "`{$i}` IN (".implode(",", $inArray).")";
171  }
172  else
173  $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
174  }
175  $query .= " WHERE ".implode(" AND ", $subQuery);
176  }
177  if (!empty($orderBy))
178  {
179  $_orderBy = array();
180  foreach ($orderBy as $i => $j)
181  {
182  if (is_numeric($i))
183  $_orderBy[] = "`{$j}` ASC";
184  else
185  {
186  $orderType = "ASC";
187  if (strtoupper($j == "DESC"))
188  $orderType = "DESC";
189  $_orderBy[] = "`{$i}` {$orderType}";
190  }
191  }
192  $query .= " ORDER BY ".implode(",", $_orderBy);
193  }
194  $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
195  if ($result === false)
196  throw new \Exception(self::$dbo->errorInfo()[2]);
197  $resultObj = array();
198  $className = get_class($this);
199  foreach ($result as $i)
200  {
201  $iObj = new $className();
202  $iObj->populate($i);
203  $resultObj[] = $iObj;
204  }
205  return $resultObj;
206  }
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: