ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables Pages
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 ()
 
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 77 of file ModelBase.php.

78  {
79  if ($key == "meta")
80  return $this->getMeta();
81  if ($key == "id")
82  return $this->id;
83  if (!isset($this->fieldsValues[$key]))
84  return null;
85  return $this->fieldsValues[$key];
86  }
getMeta($lang=null)
Definition: ModelBase.php:71
__set (   $key,
  $value 
)

Definition at line 88 of file ModelBase.php.

89  {
90  if ($value instanceof \DateTime)
91  $value = $value->format("Y-m-d");
92  $this->fieldsValues[$key] = $value;
93  if (is_bool($value))
94  $this->changed[$key] = $value ? 1 : 0;
95  else
96  $this->changed[$key] = self::$dbo->quote($value);
97  return $value;
98  }
static getDbPrefix ( )
static

Definition at line 34 of file ModelBase.php.

35  {
36  return self::$config[3];
37  }
getMeta (   $lang = null)

Definition at line 71 of file ModelBase.php.

72  {
73  $fetcher = new \Entity\Meta();
74  return $fetcher->query(array("type" => get_class()));
75  }
getTableName ( )

Definition at line 39 of file ModelBase.php.

40  {
41  $className = new \ReflectionClass($this);
42  return $this->getDbPrefix().strtolower($className->getShortName());
43  }
static getDbPrefix()
Definition: ModelBase.php:34
static init ( )
static

Definition at line 23 of file ModelBase.php.

24  {
25  if (self::$dbo !== null)
26  return true;
27  self::$config = @include("core/config.inc.php");
28  if (empty(self::$config))
29  return false;
30  self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
31  return true;
32  }
install ( )
abstractprotected
populate (   $data)
private

Definition at line 207 of file ModelBase.php.

208  {
209  $this->id = FALSE;
210  foreach ($data as $i => $j)
211  $this->fieldsValues[$i] = $j;
212  if (isset($data["id"]))
213  $this->id = (int) $data["id"];
214  }
save ( )

Definition at line 100 of file ModelBase.php.

101  {
102  \Tools\Hooks::trigger("onBeforeEntitySave");
103  \Tools\Hooks::trigger("onBeforeEntitySave".$this->getTableName());
104  if ($this->id === null)
105  {
106  if (empty ($this->changed))
107  {
108  $query = "INSERT INTO `{$this->getTableName()}` () VALUES ()";
109  $result = self::$dbo->exec($query);
110  if (!$result)
111  throw new \Exception(self::$dbo->errorInfo()[2]);
112  }
113  else
114  {
115  $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
116  $result = self::$dbo->exec($query);
117  if (!$result)
118  throw new \Exception(self::$dbo->errorInfo()[2]);
119  $this->changed = array();
120  }
121  $this->id = self::$dbo->lastInsertId();
122  }
123  else
124  {
125  if (!empty($this->changed))
126  {
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);
135  if (!$result)
136  throw new \Exception(self::$dbo->errorInfo()[2]);
137  $this->changed = array();
138  }
139  }
140  \Tools\Hooks::trigger("onAfterEntitySave");
141  \Tools\Hooks::trigger("onAfterEntitySave".$this->getTableName());
142  }
trigger($hookName, $params=null)
Definition: Hooks.php:62
selectById (   $id)

Definition at line 198 of file ModelBase.php.

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

Definition at line 144 of file ModelBase.php.

145  {
146  $query = "SELECT * FROM {$this->getTableName()}";
147 
148  if (!empty($criteria))
149  {
150  $subQuery = array();
151  foreach ($criteria as $i => $j)
152  {
153  if ($j == null)
154  $subQuery[] = "`{$i}` IS NULL";
155  else if (is_array($j))
156  {
157  $inArray = [];
158  foreach ($j as $k)
159  $inArray[] = self::$dbo->quote($k);
160  $subQuery[] = "`{$i}` IN (".implode(",", $inArray).")";
161  }
162  else
163  $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
164  }
165  $query .= " WHERE ".implode(" AND ", $subQuery);
166  }
167  if (!empty($orderBy))
168  {
169  $_orderBy = array();
170  foreach ($orderBy as $i => $j)
171  {
172  if (is_numeric($i))
173  $_orderBy[] = "`{$j}` ASC";
174  else
175  {
176  $orderType = "ASC";
177  if (strtoupper($j == "DESC"))
178  $orderType = "DESC";
179  $_orderBy[] = "`{$i}` {$orderType}";
180  }
181  }
182  $query .= " ORDER BY ".implode(",", $_orderBy);
183  }
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)
190  {
191  $iObj = new $className();
192  $iObj->populate($i);
193  $resultObj[] = $iObj;
194  }
195  return $resultObj;
196  }
static setup ( )
static

Definition at line 45 of file ModelBase.php.

46  {
47  $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module", "ModuleHook");
48 
49  self::init();
50  self::$dbo->beginTransaction();
51  try
52  {
53  foreach ($tables as $i)
54  {
55  $i = "Entity\\".$i;
56  $table = new $i();
57  if ($table->install() != true)
58  throw new \Exception();
59  }
60  }
61  catch (\Exception $e)
62  {
63  echo $e->getMessage();
64  self::$dbo->rollBack();
65  return false;
66  }
67  self::$dbo->commit();
68  return true;
69  }

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: