Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
SDAO.php
Go to the documentation of this file.
1<?php
2namespace Ubiquity\orm;
3
11
22class SDAO extends DAO {
23
24 protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) {
25 $conditionParser->limitOne();
26 $object = null;
27
28 $metaDatas = OrmUtils::getModelMetadata($className);
29 $tableName = $metaDatas['#tableName'];
30
31 $query = $db->prepareAndExecute($tableName, SqlUtils::checkWhere($conditionParser->getCondition()), self::_getFieldList($tableName, $metaDatas), $conditionParser->getParams(), $useCache, true);
32 if ($query) {
33 $object = self::sloadObjectFromRow($query, $className);
34 EventsManager::trigger(DAOEvents::GET_ONE, $object, $className);
35 }
36 return $object;
37 }
38
48 protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
49 $objects = array();
50
51 $metaDatas = OrmUtils::getModelMetadata($className);
52 $tableName = $metaDatas['#tableName'];
53
54 $query = $db->prepareAndExecute($tableName, SqlUtils::checkWhere($conditionParser->getCondition()), self::_getFieldList($tableName, $metaDatas), $conditionParser->getParams(), $useCache);
55
56 foreach ($query as $row) {
57 $objects[] = self::sloadObjectFromRow($row, $className);
58 }
59 EventsManager::trigger(DAOEvents::GET_ALL, $objects, $className);
60 return $objects;
61 }
62
63 private static function sloadObjectFromRow($row, $className) {
64 $o = new $className();
65 foreach ($row as $k => $v) {
66 $o->$k = $v;
67 }
68 $o->_rest = $row;
69 return $o;
70 }
71
81 public static function update($instance, $updateMany = false) {
82 EventsManager::trigger('dao.before.update', $instance);
83 $className = \get_class($instance);
84 $db = self::getDb($className);
85 $quote = $db->quote;
86 $tableName = OrmUtils::getTableName($className);
87 $ColumnskeyAndValues = Reflexion::getPropertiesAndValues($instance);
88 $keyFieldsAndValues = OrmUtils::getKeyFieldsAndValues($instance);
89 $sql = "UPDATE {$quote}{$tableName}{$quote} SET " . SqlUtils::getUpdateFieldsKeyAndParams($ColumnskeyAndValues) . ' WHERE ' . SqlUtils::getWhere($keyFieldsAndValues);
90 $statement = $db->getUpdateStatement($sql);
91 try {
92 $result = $statement->execute($ColumnskeyAndValues);
93 EventsManager::trigger(DAOEvents::AFTER_UPDATE, $instance, $result);
94 if (Logger::isActive()) {
95 Logger::info("DAOUpdates", $sql, "update");
96 Logger::info("DAOUpdates", \json_encode($ColumnskeyAndValues), "Key and values");
97 }
98 $instance->_rest = \array_merge($instance->_rest, $ColumnskeyAndValues);
99 return $result;
100 } catch (\Exception $e) {
101 Logger::warn("DAOUpdates", $e->getMessage(), "update");
102 }
103 return false;
104 }
105}
106
Ubiquity Generic database class.
Definition Database.php:25
SQL utilities.
Definition SqlUtils.php:13
DAO events constants.
Definition DAOEvents.php:14
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
Gateway class between database and object model.
Definition DAO.php:33
static $db
Definition DAO.php:39
static getDb($model)
Definition DAO.php:51
static getTableName($class)
Definition OrmUtils.php:45
static getKeyFieldsAndValues($instance, $prefix='')
Definition OrmUtils.php:49
static getModelMetadata($className)
Definition OrmUtils.php:21
DAO class for models without relationships Model classes must declare public members only Ubiquity\or...
Definition SDAO.php:22
static sloadObjectFromRow($row, $className)
Definition SDAO.php:63
static update($instance, $updateMany=false)
Updates an existing $instance in the database.
Definition SDAO.php:81
static _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache)
Definition SDAO.php:24
static _getAll(Database $db, $className, ConditionParser $conditionParser, $included=true, $useCache=NULL)
Definition SDAO.php:48
Represents a query condition.
Reflection utilities in dev environment only.
Definition Reflexion.php:17