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.1 |
20
|
|
|
* |
21
|
|
|
* @property array $db |
22
|
|
|
* @property boolean $useTransformers |
23
|
|
|
* @property string $transformerOp |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
trait DAOCoreTrait { |
27
|
|
|
protected static $accessors = [ ]; |
28
|
|
|
protected static $fields = [ ]; |
29
|
|
|
|
30
|
|
|
abstract public static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache): void; |
31
|
|
|
|
32
|
|
|
abstract protected static function prepareManyToMany(&$ret, $instance, $member, $annot = null); |
33
|
|
|
|
34
|
|
|
abstract protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray); |
35
|
|
|
|
36
|
|
|
abstract protected static function prepareOneToMany(&$ret, $instance, $member, $annot = null); |
37
|
|
|
|
38
|
|
|
abstract public static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields): void; |
39
|
|
|
|
40
|
|
|
abstract public static function _getIncludedForStep($included); |
41
|
|
|
|
42
|
|
|
abstract protected static function getDb($model); |
43
|
|
|
|
44
|
16 |
|
protected static function getClass_($instance) { |
45
|
16 |
|
if (is_object ( $instance )) { |
46
|
13 |
|
return get_class ( $instance ); |
47
|
|
|
} |
48
|
3 |
|
return $instance [0]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected static function getInstance_($instance) { |
52
|
|
|
if (\is_object ( $instance )) { |
53
|
|
|
return $instance; |
54
|
|
|
} |
55
|
|
|
return $instance [0]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected static function getValue_($instance, $member) { |
59
|
|
|
if (\is_object ( $instance )) { |
60
|
|
|
return Reflexion::getMemberValue ( $instance, $member ); |
61
|
|
|
} |
62
|
|
|
return $instance [1]; |
63
|
|
|
} |
64
|
|
|
|
65
|
11 |
|
protected static function getFirstKeyValue_($instance) { |
66
|
11 |
|
if (\is_object ( $instance )) { |
67
|
9 |
|
return OrmUtils::getFirstKeyValue ( $instance ); |
68
|
|
|
} |
69
|
2 |
|
return $instance [1]; |
70
|
|
|
} |
71
|
|
|
|
72
|
39 |
|
protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) { |
73
|
39 |
|
$conditionParser->limitOne (); |
74
|
39 |
|
$included = self::_getIncludedForStep ( $included ); |
75
|
39 |
|
$object = null; |
76
|
39 |
|
$invertedJoinColumns = null; |
77
|
39 |
|
$oneToManyFields = null; |
78
|
39 |
|
$manyToManyFields = null; |
79
|
|
|
|
80
|
39 |
|
$metaDatas = OrmUtils::getModelMetadata ( $className ); |
81
|
39 |
|
$tableName = $metaDatas ['#tableName']; |
82
|
39 |
|
$hasIncluded = $included || (\is_array ( $included ) && \sizeof ( $included ) > 0); |
83
|
39 |
|
if ($hasIncluded) { |
84
|
27 |
|
self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields ); |
85
|
|
|
} |
86
|
39 |
|
$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ]; |
87
|
39 |
|
$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache ); |
88
|
39 |
|
if ($query && \sizeof ( $query ) > 0) { |
89
|
39 |
|
$oneToManyQueries = [ ]; |
90
|
39 |
|
$manyToOneQueries = [ ]; |
91
|
39 |
|
$manyToManyParsers = [ ]; |
92
|
39 |
|
$accessors = $metaDatas ['#accessors']; |
93
|
39 |
|
$object = self::_loadObjectFromRow ( \current ( $query ), $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers ); |
94
|
39 |
|
if ($hasIncluded) { |
95
|
27 |
|
self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [ $object ], $included, $useCache ); |
96
|
|
|
} |
97
|
39 |
|
EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className ); |
98
|
|
|
} |
99
|
39 |
|
return $object; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
* @param Database $db |
105
|
|
|
* @param string $className |
106
|
|
|
* @param ConditionParser $conditionParser |
107
|
|
|
* @param boolean|array $included |
108
|
|
|
* @param boolean|null $useCache |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
56 |
|
protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL) { |
112
|
56 |
|
$included = self::_getIncludedForStep ( $included ); |
113
|
56 |
|
$objects = array (); |
114
|
56 |
|
$invertedJoinColumns = null; |
115
|
56 |
|
$oneToManyFields = null; |
116
|
56 |
|
$manyToManyFields = null; |
117
|
|
|
|
118
|
56 |
|
$metaDatas = OrmUtils::getModelMetadata ( $className ); |
119
|
56 |
|
$tableName = $metaDatas ['#tableName']; |
120
|
56 |
|
if ($hasIncluded = ($included || (\is_array ( $included ) && \sizeof ( $included ) > 0))) { |
121
|
24 |
|
self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields ); |
122
|
|
|
} |
123
|
56 |
|
$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ]; |
124
|
56 |
|
$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache ); |
125
|
56 |
|
$oneToManyQueries = [ ]; |
126
|
56 |
|
$manyToOneQueries = [ ]; |
127
|
56 |
|
$manyToManyParsers = [ ]; |
128
|
56 |
|
$propsKeys = OrmUtils::getPropKeys ( $className ); |
129
|
56 |
|
$accessors = $metaDatas ['#accessors']; |
130
|
56 |
|
foreach ( $query as $row ) { |
131
|
56 |
|
$object = self::_loadObjectFromRow ( $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers ); |
132
|
56 |
|
$key = OrmUtils::getPropKeyValues ( $object, $propsKeys ); |
133
|
56 |
|
$objects [$key] = $object; |
134
|
|
|
} |
135
|
56 |
|
if ($hasIncluded) { |
136
|
24 |
|
self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache ); |
137
|
|
|
} |
138
|
56 |
|
EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className ); |
139
|
56 |
|
return $objects; |
140
|
|
|
} |
141
|
|
|
|
142
|
62 |
|
public static function _getFieldList($tableName, $metaDatas) { |
143
|
62 |
|
return self::$fields [$tableName] ?? (self::$fields [$tableName] = SqlUtils::getFieldList ( \array_diff ( $metaDatas ['#fieldNames'], $metaDatas ['#notSerializable'] ), $tableName )); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* |
148
|
|
|
* @param array $row |
149
|
|
|
* @param string $className |
150
|
|
|
* @param array $invertedJoinColumns |
151
|
|
|
* @param array $manyToOneQueries |
152
|
|
|
* @param array $accessors |
153
|
|
|
* @return object |
154
|
|
|
*/ |
155
|
62 |
|
public static function _loadObjectFromRow($row, $className, &$invertedJoinColumns, &$manyToOneQueries, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, &$accessors, &$transformers) { |
156
|
62 |
|
$o = new $className (); |
157
|
62 |
|
if (self::$useTransformers) { |
158
|
8 |
|
foreach ( $transformers as $field => $transformer ) { |
159
|
4 |
|
$transform = self::$transformerOp; |
160
|
4 |
|
$row [$field] = $transformer::$transform ( $row [$field] ); |
161
|
|
|
} |
162
|
|
|
} |
163
|
62 |
|
foreach ( $row as $k => $v ) { |
164
|
62 |
|
if (isset ( $accessors [$k] )) { |
165
|
62 |
|
$accesseur = $accessors [$k]; |
166
|
62 |
|
$o->$accesseur ( $v ); |
167
|
|
|
} |
168
|
62 |
|
$o->_rest [$k] = $v; |
169
|
62 |
|
if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) { |
170
|
23 |
|
$fk = '_' . $k; |
171
|
23 |
|
$o->$fk = $v; |
172
|
23 |
|
self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] ); |
173
|
|
|
} |
174
|
|
|
} |
175
|
62 |
|
if (isset ( $oneToManyFields )) { |
176
|
40 |
|
foreach ( $oneToManyFields as $k => $annot ) { |
177
|
37 |
|
self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot ); |
178
|
|
|
} |
179
|
|
|
} |
180
|
62 |
|
if (isset ( $manyToManyFields )) { |
181
|
23 |
|
foreach ( $manyToManyFields as $k => $annot ) { |
182
|
23 |
|
self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot ); |
183
|
|
|
} |
184
|
|
|
} |
185
|
62 |
|
return $o; |
186
|
|
|
} |
187
|
|
|
|
188
|
3 |
|
private static function parseKey(&$keyValues, $className, $quote) { |
189
|
3 |
|
if (! \is_array ( $keyValues )) { |
190
|
3 |
|
if (\strrpos ( $keyValues, '=' ) === false && \strrpos ( $keyValues, '>' ) === false && \strrpos ( $keyValues, '<' ) === false) { |
191
|
3 |
|
$keyValues = $quote . OrmUtils::getFirstKey ( $className ) . $quote . "='" . $keyValues . "'"; |
192
|
|
|
} |
193
|
|
|
} |
194
|
3 |
|
} |
195
|
|
|
} |
196
|
|
|
|