All Data Structures Namespaces Files Functions Variables
ModelBase Class Reference
Inheritance diagram for ModelBase:

## 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.

{
self::init();
$this->id = null;
if ($id !== null && is_numeric($id))
$this->selectById($id);
}
Entity\ModelBase\$id
$id
Definition: ModelBase.php:11
Entity\ModelBase\selectById
selectById($id)
Definition: ModelBase.php:230

## Member Function Documentation

__get | ( |  | $key | ) |

Definition at line 85 of file ModelBase.php.

{
if ($key == "meta")
return $this->getMeta();
if ($key == "id")
return $this->id === null ? null : (int) $this->id;
if (!isset($this->fieldsValues[$key]))
return null;
return $this->fieldsValues[$key];
}
Entity\ModelBase\getMeta
getMeta($lang=null)
Definition: ModelBase.php:79

__set | ( |  | $key,
|  |  | $value
| ) |  |

Definition at line 96 of file ModelBase.php.

{
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 if ($value === null)
$this->changed[$key] = "NULL";
else
$this->changed[$key] = self::$dbo->quote($value);
return $value;
}

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.

{
if ($this->id === false)
return true;
\Tools\Hooks::trigger("onBeforeEntityDelete");
\Tools\Hooks::trigger("onBeforeEntityDelete".$this->getTableName());
$id = (int) $this->id;
foreach ($this->fieldsValues as $i => $j)
if (!isset($this->changed[$i]))
$this->changed[$i] = $j;
if (self::$dbo->exec("DELETE FROM {$this->getTableName()} WHERE `id`={$id}") === false)
return false;
\Tools\Hooks::trigger("onAfterEntityDelete");
\Tools\Hooks::trigger("onAfterEntityDelete".$this->getTableName());
return true;
}
Entity\ModelBase\$id
$id
Definition: ModelBase.php:11
Tools\Hooks\trigger
trigger($hookName, $params=null)
Definition: Hooks.php:70
Entity\ModelBase\getTableName
getTableName()
Definition: ModelBase.php:47

static getDbPrefix | ( |  | ) |

static

Definition at line 42 of file ModelBase.php.

{
return self::$config[3];
}

getMeta | ( |  | $lang = `null` | ) |

Definition at line 79 of file ModelBase.php.

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

getTableName | ( |  | ) |

Definition at line 47 of file ModelBase.php.

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

static init | ( |  | $config = `null` | ) |

static

Definition at line 23 of file ModelBase.php.

{
if (self::$dbo !== null)
return true;
// @codeCoverageIgnoreStart
// Load config from written file; this file is created from setup script
if ($config === null && self::$config === null)
{
self::$config = @include("core/config.inc.php");
if (empty(self::$config))
return false;
}
// @codeCoverageIgnoreEnd
else
self::$config = $config;
self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
return true;
}
Entity\ModelBase\$config
static $config
Definition: ModelBase.php:10

install | ( |  | ) |

abstractprotected

populate | ( |  | $data | ) |

private

Definition at line 239 of file ModelBase.php.

{
$this->id = FALSE;
if ($data === false)
throw new \Exception("Cannot load entity: no result found");
foreach ($data as $i => $j)
$this->fieldsValues[$i] = $j;
if (isset($this->fieldsValues["id"]))
$this->id = (int) $this->fieldsValues["id"];
$this->changed = array();
}

save | ( |  | ) |

Definition at line 110 of file ModelBase.php.

{
\Tools\Hooks::trigger("onBeforeEntitySave");
\Tools\Hooks::trigger("onBeforeEntitySave".$this->getTableName());
if ($this->id === null)
{
if (empty ($this->changed))
{
$query = "INSERT INTO `{$this->getTableName()}` () VALUES ()";
$result = self::$dbo->exec($query);
if (!$result)
throw new \Exception(self::$dbo->errorInfo()[2]);
}
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();
}
$this->id = self::$dbo->lastInsertId();
}
else
{
if (!empty($this->changed))
{
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}`=" . $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();
}
}
\Tools\Hooks::trigger("onAfterEntitySave");
\Tools\Hooks::trigger("onAfterEntitySave".$this->getTableName());
}
Tools\Hooks\trigger
trigger($hookName, $params=null)
Definition: Hooks.php:70
Entity\ModelBase\getTableName
getTableName()
Definition: ModelBase.php:47

selectById | ( |  | $id | ) |

Definition at line 230 of file ModelBase.php.

{
$query = "SELECT * FROM {$this->getTableName()} WHERE id=".(int)$id." LIMIT 1";
$result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
if ($result === false || empty($result))
throw new \Exception("Cannot fetch data: ".self::$dbo->errorInfo()[2]);
$this->populate($result->fetch());
}
Entity\ModelBase\$id
$id
Definition: ModelBase.php:11
Entity\ModelBase\populate
populate($data)
Definition: ModelBase.php:239

selects | ( |  | $criteria = `null`,
|  |  | $orderBy = `null`
| ) |  |

Definition at line 176 of file ModelBase.php.

{
$query = "SELECT * FROM {$this->getTableName()}";
if (!empty($criteria))
{
$subQuery = array();
foreach ($criteria as $i => $j)
{
if ($j == null)
$subQuery[] = "`{$i}` IS NULL";
else if (is_array($j))
{
$inArray = [];
foreach ($j as $k)
$inArray[] = self::$dbo->quote($k);
$subQuery[] = "`{$i}` IN (".implode(",", $inArray).")";
}
else
$subQuery[] = "`{$i}`=".self::$dbo->quote($j);
}
$query .= " WHERE ".implode(" AND ", $subQuery);
}
if (!empty($orderBy))
{
$_orderBy = array();
foreach ($orderBy as $i => $j)
{
if (is_numeric($i))
$_orderBy[] = "`{$j}` ASC";
else
{
$orderType = "ASC";
if (strtoupper($j == "DESC"))
$orderType = "DESC";
$_orderBy[] = "`{$i}` {$orderType}";
}
}
$query .= " ORDER BY ".implode(",", $_orderBy);
}
$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();
$iObj->populate($i);
$resultObj[] = $iObj;
}
return $resultObj;
}

static setup | ( |  | ) |

static

Definition at line 53 of file ModelBase.php.

{
$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("{$i}: Cannot table setup failure");
}
}
catch (\Exception $e)
{
self::$dbo->rollBack();
error_log($e->getMessage());
return false;
}
self::$dbo->commit();
return true;
}
Exception
Definition: Error404.php:3

## 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:

- core/models/ModelBase.php