Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAOCoreTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm\traits;
4
12
28 protected static $accessors = [];
29 protected static $fields = [];
30
31 abstract public static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache): void;
32
33 abstract protected static function prepareManyToMany($db, &$ret, $instance, $member, $annot = null);
34
35 abstract protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray);
36
37 abstract protected static function prepareOneToMany(&$ret, $instance, $member, $annot = null);
38
39 abstract public static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields): void;
40
41 abstract public static function _getIncludedForStep($included);
42
43 abstract protected static function getDb($model);
44
45 protected static function getClass_($instance) {
46 if (\is_object($instance)) {
47 return get_class($instance);
48 }
49 return $instance [0];
50 }
51
52 protected static function getInstance_($instance) {
53 if (\is_object($instance)) {
54 return $instance;
55 }
56 return $instance [0];
57 }
58
59 protected static function getValue_($instance, $member) {
60 if (\is_object($instance)) {
61 return Reflexion::getMemberValue($instance, $member);
62 }
63 return $instance [1];
64 }
65
66 protected static function getFirstKeyValue_($instance) {
67 if (\is_object($instance)) {
68 return OrmUtils::getFirstKeyValue($instance);
69 }
70 return $instance [1];
71 }
72
73 protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) {
74 $conditionParser->limitOne();
75 $included = self::_getIncludedForStep($included);
76 $object = $invertedJoinColumns = $oneToManyFields = $manyToManyFields = null;
77
78 $metaDatas = OrmUtils::getModelMetadata($className);
79 $tableName = $metaDatas ['#tableName'];
80 $hasIncluded = $included || (\is_array($included) && \count($included) > 0);
81 if ($hasIncluded) {
82 self::_initRelationFields($included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields);
83 }
84 $transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [];
85 $query = $db->prepareAndExecute($tableName, SqlUtils::checkWhere($conditionParser->getCondition()), self::_getFieldList($tableName, $metaDatas), $conditionParser->getParams(), $useCache, true);
86 if ($query) {
87 $oneToManyQueries = $manyToOneQueries = $manyToManyParsers = [];
88 $object = self::_loadObjectFromRow($db, $query, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $metaDatas ['#memberNames'] ?? null, $metaDatas ['#accessors'], $transformers, $metaDatas['#primaryKeys'] ?? []);
89 if ($hasIncluded) {
90 self::_affectsRelationObjects($className, OrmUtils::getFirstPropKey($className), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [$object], $included, $useCache);
91 }
92 EventsManager::trigger(DAOEvents::GET_ONE, $object, $className);
93 }
94 return $object;
95 }
96
106 protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = null) {
107 $included = self::_getIncludedForStep($included);
108 $objects = [];
109 $invertedJoinColumns = $oneToManyFields = $manyToManyFields = null;
110
111 $metaDatas = OrmUtils::getModelMetadata($className);
112 $primaryKeys = $metaDatas['#primaryKeys'] ?? [];
113 $tableName = $metaDatas ['#tableName'];
114 if ($hasIncluded = ($included || (\is_array($included) && \count($included) > 0))) {
115 self::_initRelationFields($included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields);
116 }
117 $transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [];
118 $query = $db->prepareAndExecute($tableName, SqlUtils::checkWhere($conditionParser->getCondition()), self::_getFieldList($tableName, $metaDatas), $conditionParser->getParams(), $useCache);
119
120 $oneToManyQueries = $manyToOneQueries = $manyToManyParsers = [];
121
122 $propsKeys = OrmUtils::getPropKeys($className);
123 foreach ($query as $row) {
124 $object = self::_loadObjectFromRow($db, $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $metaDatas ['#memberNames'] ?? null, $metaDatas ['#accessors'], $transformers, $primaryKeys);
125 $objects [OrmUtils::getPropKeyValues($object, $propsKeys)] = $object;
126 }
127 if ($hasIncluded) {
128 self::_affectsRelationObjects($className, OrmUtils::getFirstPropKey($className), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache);
129 }
130 EventsManager::trigger(DAOEvents::GET_ALL, $objects, $className);
131 return $objects;
132 }
133
134 public static function _getFieldList($tableName, $metaDatas) {
135 return self::$fields [$tableName] ??= SqlUtils::getFieldList(\array_diff($metaDatas ['#fieldNames'], $metaDatas ['#notSerializable']), $tableName);
136 }
137
154 public static function _loadObjectFromRow(Database $db, $row, $className, $invertedJoinColumns, &$manyToOneQueries, $oneToManyFields, $manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, $memberNames, $accessors, $transformers, $primaryKeys) {
155 $o = new $className ();
156 if (self::$useTransformers) {
157 self::applyTransformers($transformers, $row, $memberNames);
158 }
159 foreach ($row as $k => $v) {
160 if ($accesseur = ($accessors [$k] ?? false)) {
161 $o->$accesseur ($v);
162 }
163 $o->_rest [$memberNames [$k] ?? $k] = $v;
164 if (isset($primaryKeys[$k])) {
165 $o->_pkv['___' . $k] = $v;
166 }
167 if (isset ($invertedJoinColumns) && isset ($invertedJoinColumns [$k])) {
168 $fk = '_' . $k;
169 $o->$fk = $v;
170 self::prepareManyToOne($manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k]);
171 }
172 }
173 self::loadManys($o, $db, $oneToManyFields, $oneToManyQueries, $manyToManyFields, $manyToManyParsers);
174 return $o;
175 }
176
186 public static function _loadSimpleObjectFromRow(Database $db, $row, $className, $memberNames, $transformers) {
187 $o = new $className ();
188 if (self::$useTransformers) {
189 self::applyTransformers($transformers, $row, $memberNames);
190 }
191 foreach ($row as $k => $v) {
192 $o->$k = $v;
193 $o->_rest [$memberNames [$k] ?? $k] = $v;
194 }
195 return $o;
196 }
197
198 protected static function applyTransformers($transformers, &$row, $memberNames) {
199 foreach ($transformers as $member => $transformer) {
200 $field = \array_search($member, $memberNames);
201 $transform = self::$transformerOp;
202 $row [$field] = $transformer::{$transform} ($row [$field]);
203 }
204 }
205
206 protected static function loadManys($o, $db, $oneToManyFields, &$oneToManyQueries, $manyToManyFields, &$manyToManyParsers) {
207 if (isset ($oneToManyFields)) {
208 foreach ($oneToManyFields as $k => $annot) {
209 self::prepareOneToMany($oneToManyQueries, $o, $k, $annot);
210 }
211 }
212 if (isset ($manyToManyFields)) {
213 foreach ($manyToManyFields as $k => $annot) {
214 self::prepareManyToMany($db, $manyToManyParsers, $o, $k, $annot);
215 }
216 }
217 }
218
219 private static function parseKey(&$keyValues, $className, $quote) {
220 if (!\is_array($keyValues)) {
221 if (\strrpos($keyValues, '=') === false && \strrpos($keyValues, '>') === false && \strrpos($keyValues, '<') === false) {
222 $keyValues = $quote . OrmUtils::getFirstKey($className) . $quote . "='" . $keyValues . "'";
223 }
224 }
225 }
226
227 public static function storeDbCache(string $model) {
228 $offset = self::$modelsDatabase [$model] ?? 'default';
229 if (isset (self::$db [$offset])) {
230 self::$db [$offset]->storeCache();
231 }
232 }
233
234 public static function getModels($dbOffset = 'default') {
235 $result = [];
236 foreach (self::$modelsDatabase as $model => $offset) {
237 if ($offset === $dbOffset) {
238 $result[] = $model;
239 }
240 }
241 return $result;
242 }
243}
Ubiquity Generic database class.
Definition Database.php:25
SQL utilities.
Definition SqlUtils.php:13
prepareAndExecute($tableName, $condition, $fields, $parameters=null, $useCache=false, $one=false)
DAO events constants.
Definition DAOEvents.php:14
Object/relational mapping utilities.
Definition OrmUtils.php:17
static getPropKeyValues($instance, $props)
Definition OrmUtils.php:121
static getFirstKeyValue($instance)
Definition OrmUtils.php:107
static getModelMetadata($className)
Definition OrmUtils.php:21
Represents a query condition.
Reflection utilities in dev environment only.
Definition Reflexion.php:17
static _getIncludedForStep($included)
static prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray)
static prepareOneToMany(&$ret, $instance, $member, $annot=null)
static _loadObjectFromRow(Database $db, $row, $className, $invertedJoinColumns, &$manyToOneQueries, $oneToManyFields, $manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, $memberNames, $accessors, $transformers, $primaryKeys)
static prepareManyToMany($db, &$ret, $instance, $member, $annot=null)
static _getFieldList($tableName, $metaDatas)
static _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields)
static getFirstKeyValue_($instance)
static _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache)
static getValue_($instance, $member)
static getModels($dbOffset='default')
static _loadSimpleObjectFromRow(Database $db, $row, $className, $memberNames, $transformers)
static _getAll(Database $db, $className, ConditionParser $conditionParser, $included=true, $useCache=null)
static storeDbCache(string $model)
static loadManys($o, $db, $oneToManyFields, &$oneToManyQueries, $manyToManyFields, &$manyToManyParsers)
static parseKey(&$keyValues, $className, $quote)
static applyTransformers($transformers, &$row, $memberNames)
static _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache)
Ubiquity\orm\traits This class is part of Ubiquity.