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
|
|
|
use Ubiquity\orm\core\DAOPreparedQuery; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Core Trait for DAO class. |
16
|
|
|
* Ubiquity\orm\traits$DAOCoreTrait |
17
|
|
|
* This class is part of Ubiquity |
18
|
|
|
* |
19
|
|
|
* @author jcheron <[email protected]> |
20
|
|
|
* @version 1.1.1 |
21
|
|
|
* |
22
|
|
|
* @property array $db |
23
|
|
|
* @property boolean $useTransformers |
24
|
|
|
* @property string $transformerOp |
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(&$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
|
16 |
|
|
45
|
16 |
|
protected static function getClass_($instance) { |
46
|
13 |
|
if (is_object ( $instance )) { |
47
|
|
|
return get_class ( $instance ); |
48
|
3 |
|
} |
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
|
11 |
|
|
66
|
11 |
|
protected static function getFirstKeyValue_($instance) { |
67
|
9 |
|
if (\is_object ( $instance )) { |
68
|
|
|
return OrmUtils::getFirstKeyValue ( $instance ); |
69
|
2 |
|
} |
70
|
|
|
return $instance [1]; |
71
|
|
|
} |
72
|
39 |
|
|
73
|
39 |
|
protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) { |
74
|
39 |
|
$conditionParser->limitOne (); |
75
|
39 |
|
$included = self::_getIncludedForStep ( $included ); |
76
|
39 |
|
$object = null; |
77
|
39 |
|
$invertedJoinColumns = null; |
78
|
39 |
|
$oneToManyFields = null; |
79
|
|
|
$manyToManyFields = null; |
80
|
39 |
|
|
81
|
39 |
|
$metaDatas = OrmUtils::getModelMetadata ( $className ); |
82
|
39 |
|
$tableName = $metaDatas ['#tableName']; |
83
|
39 |
|
$hasIncluded = $included || (\is_array ( $included ) && \sizeof ( $included ) > 0); |
84
|
27 |
|
if ($hasIncluded) { |
85
|
|
|
self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields ); |
86
|
39 |
|
} |
87
|
39 |
|
$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ]; |
88
|
39 |
|
$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache ); |
89
|
39 |
|
if ($query && \sizeof ( $query ) > 0) { |
90
|
39 |
|
$oneToManyQueries = [ ]; |
91
|
39 |
|
$manyToOneQueries = [ ]; |
92
|
39 |
|
$manyToManyParsers = [ ]; |
93
|
39 |
|
$accessors = $metaDatas ['#accessors']; |
94
|
39 |
|
$object = self::_loadObjectFromRow ( \current ( $query ), $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers ); |
95
|
27 |
|
if ($hasIncluded) { |
96
|
|
|
self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [ $object ], $included, $useCache ); |
97
|
39 |
|
} |
98
|
|
|
EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className ); |
99
|
39 |
|
} |
100
|
|
|
return $object; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @param Database $db |
106
|
|
|
* @param string $className |
107
|
|
|
* @param ConditionParser $conditionParser |
108
|
|
|
* @param boolean|array $included |
109
|
|
|
* @param boolean|null $useCache |
110
|
|
|
* @return array |
111
|
56 |
|
*/ |
112
|
56 |
|
protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL) { |
113
|
56 |
|
$included = self::_getIncludedForStep ( $included ); |
114
|
56 |
|
$objects = array (); |
115
|
56 |
|
$invertedJoinColumns = null; |
116
|
56 |
|
$oneToManyFields = null; |
117
|
|
|
$manyToManyFields = null; |
118
|
56 |
|
|
119
|
56 |
|
$metaDatas = OrmUtils::getModelMetadata ( $className ); |
120
|
56 |
|
$tableName = $metaDatas ['#tableName']; |
121
|
56 |
|
if ($hasIncluded = $included || (\is_array ( $included ) && \sizeof ( $included ) > 0)) { |
|
|
|
|
122
|
24 |
|
self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields ); |
123
|
|
|
} |
124
|
56 |
|
$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ]; |
125
|
56 |
|
$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::_getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache ); |
126
|
56 |
|
$oneToManyQueries = [ ]; |
127
|
56 |
|
$manyToOneQueries = [ ]; |
128
|
56 |
|
$manyToManyParsers = [ ]; |
129
|
56 |
|
$propsKeys = OrmUtils::getPropKeys ( $className ); |
130
|
56 |
|
$accessors = $metaDatas ['#accessors']; |
131
|
56 |
|
foreach ( $query as $row ) { |
132
|
56 |
|
$object = self::_loadObjectFromRow ( $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers ); |
133
|
56 |
|
$key = OrmUtils::getPropKeyValues ( $object, $propsKeys ); |
134
|
56 |
|
$objects [$key] = $object; |
135
|
|
|
} |
136
|
56 |
|
if ($hasIncluded) { |
137
|
24 |
|
self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache ); |
138
|
|
|
} |
139
|
56 |
|
EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className ); |
140
|
56 |
|
return $objects; |
141
|
|
|
} |
142
|
|
|
|
143
|
62 |
|
public static function _getFieldList($tableName, $metaDatas) { |
144
|
62 |
|
return self::$fields [$tableName] ?? (self::$fields [$tableName] = SqlUtils::getFieldList ( \array_diff ( $metaDatas ['#fieldNames'], $metaDatas ['#notSerializable'] ), $tableName )); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* |
149
|
|
|
* @param array $row |
150
|
|
|
* @param string $className |
151
|
|
|
* @param array $invertedJoinColumns |
152
|
|
|
* @param array $manyToOneQueries |
153
|
|
|
* @param array $accessors |
154
|
|
|
* @return object |
155
|
|
|
*/ |
156
|
62 |
|
public static function _loadObjectFromRow($row, $className, &$invertedJoinColumns, &$manyToOneQueries, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, &$accessors, &$transformers) { |
157
|
62 |
|
$o = new $className (); |
158
|
62 |
|
if (self::$useTransformers) { |
159
|
8 |
|
foreach ( $transformers as $field => $transformer ) { |
160
|
4 |
|
$transform = self::$transformerOp; |
161
|
4 |
|
$row [$field] = $transformer::$transform ( $row [$field] ); |
162
|
|
|
} |
163
|
|
|
} |
164
|
62 |
|
foreach ( $row as $k => $v ) { |
165
|
62 |
|
if (isset ( $accessors [$k] )) { |
166
|
62 |
|
$accesseur = $accessors [$k]; |
167
|
62 |
|
$o->$accesseur ( $v ); |
168
|
|
|
} |
169
|
62 |
|
$o->_rest [$k] = $v; |
170
|
62 |
|
if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) { |
171
|
23 |
|
$fk = '_' . $k; |
172
|
23 |
|
$o->$fk = $v; |
173
|
62 |
|
self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] ); |
174
|
|
|
} |
175
|
|
|
} |
176
|
62 |
|
if (isset ( $oneToManyFields )) { |
177
|
40 |
|
foreach ( $oneToManyFields as $k => $annot ) { |
178
|
37 |
|
self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot ); |
179
|
|
|
} |
180
|
|
|
} |
181
|
62 |
|
if (isset ( $manyToManyFields )) { |
182
|
23 |
|
foreach ( $manyToManyFields as $k => $annot ) { |
183
|
23 |
|
self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot ); |
184
|
|
|
} |
185
|
|
|
} |
186
|
62 |
|
return $o; |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
private static function parseKey(&$keyValues, $className, $quote) { |
190
|
3 |
|
if (! \is_array ( $keyValues )) { |
191
|
3 |
|
if (\strrpos ( $keyValues, '=' ) === false && \strrpos ( $keyValues, '>' ) === false && \strrpos ( $keyValues, '<' ) === false) { |
192
|
3 |
|
$keyValues = $quote . OrmUtils::getFirstKey ( $className ) . $quote . "='" . $keyValues . "'"; |
193
|
|
|
} |
194
|
|
|
} |
195
|
3 |
|
} |
196
|
|
|
} |
197
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths