|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\db\SqlUtils; |
|
6
|
|
|
use Ubiquity\events\DAOEvents; |
|
7
|
|
|
use Ubiquity\events\EventsManager; |
|
8
|
|
|
use Ubiquity\orm\OrmUtils; |
|
9
|
|
|
use Ubiquity\orm\parser\ConditionParser; |
|
10
|
|
|
use Ubiquity\orm\parser\Reflexion; |
|
11
|
|
|
use Ubiquity\db\Database; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Core Trait for DAO class. |
|
15
|
|
|
* Ubiquity\orm\traits$DAOCoreTrait |
|
16
|
|
|
* This class is part of Ubiquity |
|
17
|
|
|
* |
|
18
|
|
|
* @author jcheron <[email protected]> |
|
19
|
|
|
* @version 1.1.6 |
|
20
|
|
|
* |
|
21
|
|
|
* @property array $db |
|
22
|
|
|
* @property boolean $useTransformers |
|
23
|
|
|
* @property string $transformerOp |
|
24
|
|
|
* @property array $modelsDatabase |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
trait DAOCoreTrait { |
|
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
|
17 |
|
|
|
45
|
17 |
|
protected static function getClass_($instance) { |
|
46
|
16 |
|
if (\is_object ( $instance )) { |
|
47
|
|
|
return get_class ( $instance ); |
|
48
|
1 |
|
} |
|
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
|
12 |
|
|
|
66
|
12 |
|
protected static function getFirstKeyValue_($instance) { |
|
67
|
11 |
|
if (\is_object ( $instance )) { |
|
68
|
|
|
return OrmUtils::getFirstKeyValue ( $instance ); |
|
69
|
1 |
|
} |
|
70
|
|
|
return $instance [1]; |
|
71
|
|
|
} |
|
72
|
50 |
|
|
|
73
|
50 |
|
protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) { |
|
74
|
50 |
|
$conditionParser->limitOne (); |
|
75
|
50 |
|
$included = self::_getIncludedForStep ( $included ); |
|
76
|
|
|
$object = $invertedJoinColumns = $oneToManyFields = $manyToManyFields = null; |
|
77
|
50 |
|
|
|
78
|
50 |
|
$metaDatas = OrmUtils::getModelMetadata ( $className ); |
|
79
|
50 |
|
$tableName = $metaDatas ['#tableName']; |
|
80
|
50 |
|
$hasIncluded = $included || (\is_array ( $included ) && \count ( $included ) > 0); |
|
81
|
33 |
|
if ($hasIncluded) { |
|
82
|
|
|
self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields ); |
|
83
|
50 |
|
} |
|
84
|
50 |
|
$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ]; |
|
85
|
50 |
|
$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache, true ); |
|
86
|
50 |
|
if ($query) { |
|
87
|
50 |
|
$oneToManyQueries = $manyToOneQueries = $manyToManyParsers = [ ]; |
|
88
|
50 |
|
$object = self::_loadObjectFromRow ( $db, $query, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $metaDatas ['#memberNames'] ?? null, $metaDatas ['#accessors'], $transformers ); |
|
89
|
33 |
|
if ($hasIncluded) { |
|
90
|
|
|
self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [ $object ], $included, $useCache ); |
|
91
|
50 |
|
} |
|
92
|
|
|
EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className ); |
|
93
|
50 |
|
} |
|
94
|
|
|
return $object; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* |
|
99
|
|
|
* @param Database $db |
|
100
|
|
|
* @param string $className |
|
101
|
|
|
* @param ConditionParser $conditionParser |
|
102
|
|
|
* @param boolean|array $included |
|
103
|
|
|
* @param boolean|null $useCache |
|
104
|
|
|
* @return array |
|
105
|
77 |
|
*/ |
|
106
|
77 |
|
protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL) { |
|
107
|
77 |
|
$included = self::_getIncludedForStep ( $included ); |
|
108
|
77 |
|
$objects = []; |
|
109
|
|
|
$invertedJoinColumns =$oneToManyFields = $manyToManyFields = null; |
|
110
|
77 |
|
|
|
111
|
77 |
|
$metaDatas = OrmUtils::getModelMetadata ( $className ); |
|
112
|
77 |
|
$tableName = $metaDatas ['#tableName']; |
|
113
|
31 |
|
if ($hasIncluded = ($included || (\is_array ( $included ) && \count ( $included ) > 0))) { |
|
114
|
|
|
self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields ); |
|
115
|
77 |
|
} |
|
116
|
77 |
|
$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ]; |
|
117
|
|
|
$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache ); |
|
118
|
77 |
|
|
|
119
|
|
|
$oneToManyQueries = $manyToOneQueries = $manyToManyParsers = [ ]; |
|
120
|
77 |
|
|
|
121
|
77 |
|
$propsKeys = OrmUtils::getPropKeys ( $className ); |
|
122
|
77 |
|
foreach ( $query as $row ) { |
|
123
|
77 |
|
$object = self::_loadObjectFromRow ( $db, $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $metaDatas ['#memberNames'] ?? null, $metaDatas ['#accessors'], $transformers ); |
|
124
|
|
|
$objects [OrmUtils::getPropKeyValues ( $object, $propsKeys )] = $object; |
|
125
|
77 |
|
} |
|
126
|
31 |
|
if ($hasIncluded) { |
|
127
|
|
|
self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache ); |
|
128
|
77 |
|
} |
|
129
|
77 |
|
EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className ); |
|
130
|
|
|
return $objects; |
|
131
|
|
|
} |
|
132
|
122 |
|
|
|
133
|
122 |
|
public static function _getFieldList($tableName, $metaDatas) { |
|
134
|
|
|
return self::$fields [$tableName] ??= SqlUtils::getFieldList ( \array_diff ( $metaDatas ['#fieldNames'], $metaDatas ['#notSerializable'] ), $tableName ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* |
|
139
|
|
|
* @param Database $db |
|
140
|
|
|
* @param array $row |
|
141
|
|
|
* @param string $className |
|
142
|
|
|
* @param array $invertedJoinColumns |
|
143
|
|
|
* @param array $manyToOneQueries |
|
144
|
|
|
* @param array $oneToManyFields |
|
145
|
|
|
* @param array $manyToManyFields |
|
146
|
|
|
* @param array $oneToManyQueries |
|
147
|
|
|
* @param array $manyToManyParsers |
|
148
|
|
|
* @param array $memberNames |
|
149
|
|
|
* @param array $accessors |
|
150
|
|
|
* @param array $transformers |
|
151
|
|
|
* @return object |
|
152
|
86 |
|
*/ |
|
153
|
86 |
|
public static function _loadObjectFromRow(Database $db, $row, $className, $invertedJoinColumns, &$manyToOneQueries, $oneToManyFields, $manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, $memberNames, $accessors, $transformers) { |
|
154
|
86 |
|
$o = new $className (); |
|
155
|
8 |
|
if (self::$useTransformers) { |
|
156
|
|
|
self::applyTransformers ( $transformers, $row, $memberNames ); |
|
157
|
86 |
|
} |
|
158
|
86 |
|
foreach ( $row as $k => $v ) { |
|
159
|
86 |
|
if ($accesseur = ($accessors [$k] ?? false)) { |
|
160
|
|
|
$o->$accesseur ( $v ); |
|
161
|
86 |
|
} |
|
162
|
86 |
|
$o->_rest [$memberNames [$k] ?? $k] = $v; |
|
163
|
25 |
|
if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) { |
|
164
|
25 |
|
$fk = '_' . $k; |
|
165
|
25 |
|
$o->$fk = $v; |
|
166
|
|
|
self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] ); |
|
167
|
|
|
} |
|
168
|
86 |
|
} |
|
169
|
86 |
|
self::loadManys ( $o, $db, $oneToManyFields, $oneToManyQueries, $manyToManyFields, $manyToManyParsers ); |
|
170
|
|
|
return $o; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* |
|
175
|
|
|
* @param Database $db |
|
176
|
|
|
* @param array $row |
|
177
|
|
|
* @param string $className |
|
178
|
|
|
* @param array $memberNames |
|
179
|
|
|
* @param array $transformers |
|
180
|
|
|
* @return object |
|
181
|
4 |
|
*/ |
|
182
|
4 |
|
public static function _loadSimpleObjectFromRow(Database $db, $row, $className, $memberNames, $transformers) { |
|
183
|
4 |
|
$o = new $className (); |
|
184
|
|
|
if (self::$useTransformers) { |
|
185
|
|
|
self::applyTransformers ( $transformers, $row, $memberNames ); |
|
186
|
4 |
|
} |
|
187
|
4 |
|
foreach ( $row as $k => $v ) { |
|
188
|
4 |
|
$o->$k = $v; |
|
189
|
|
|
$o->_rest [$memberNames [$k] ?? $k] = $v; |
|
190
|
4 |
|
} |
|
191
|
|
|
return $o; |
|
192
|
|
|
} |
|
193
|
8 |
|
|
|
194
|
8 |
|
protected static function applyTransformers($transformers, &$row, $memberNames) { |
|
195
|
7 |
|
foreach ( $transformers as $member => $transformer ) { |
|
196
|
7 |
|
$field = \array_search ( $member, $memberNames ); |
|
197
|
7 |
|
$transform = self::$transformerOp; |
|
198
|
|
|
$row [$field] = $transformer::{$transform} ( $row [$field] ); |
|
199
|
8 |
|
} |
|
200
|
|
|
} |
|
201
|
86 |
|
|
|
202
|
86 |
|
protected static function loadManys($o, $db, $oneToManyFields, &$oneToManyQueries, $manyToManyFields, &$manyToManyParsers) { |
|
203
|
48 |
|
if (isset ( $oneToManyFields )) { |
|
204
|
44 |
|
foreach ( $oneToManyFields as $k => $annot ) { |
|
205
|
|
|
self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot ); |
|
206
|
|
|
} |
|
207
|
86 |
|
} |
|
208
|
26 |
|
if (isset ( $manyToManyFields )) { |
|
209
|
25 |
|
foreach ( $manyToManyFields as $k => $annot ) { |
|
210
|
|
|
self::prepareManyToMany ( $db, $manyToManyParsers, $o, $k, $annot ); |
|
211
|
|
|
} |
|
212
|
86 |
|
} |
|
213
|
|
|
} |
|
214
|
4 |
|
|
|
215
|
4 |
|
private static function parseKey(&$keyValues, $className, $quote) { |
|
216
|
4 |
|
if (! \is_array ( $keyValues )) { |
|
217
|
4 |
|
if (\strrpos ( $keyValues, '=' ) === false && \strrpos ( $keyValues, '>' ) === false && \strrpos ( $keyValues, '<' ) === false) { |
|
218
|
|
|
$keyValues = $quote . OrmUtils::getFirstKey ( $className ) . $quote . "='" . $keyValues . "'"; |
|
219
|
|
|
} |
|
220
|
4 |
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public static function storeDbCache(string $model) { |
|
224
|
|
|
$offset = self::$modelsDatabase [$model] ?? 'default'; |
|
225
|
|
|
if (isset ( self::$db [$offset] )) { |
|
226
|
|
|
self::$db [$offset]->storeCache (); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public static function getModels($dbOffset='default'){ |
|
231
|
|
|
$result=[]; |
|
232
|
|
|
foreach ( self::$modelsDatabase as $model=>$offset){ |
|
233
|
|
|
if($offset===$dbOffset){ |
|
234
|
|
|
$result[]=$model; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
return $result; |
|
238
|
|
|
} |
|
239
|
|
|
} |