1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\db\Database; |
6
|
|
|
use Ubiquity\db\SqlUtils; |
7
|
|
|
use Ubiquity\log\Logger; |
8
|
|
|
use Ubiquity\orm\OrmUtils; |
9
|
|
|
use Ubiquity\orm\parser\ConditionParser; |
10
|
|
|
use Ubiquity\orm\parser\ManyToManyParser; |
11
|
|
|
use Ubiquity\orm\parser\Reflexion; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @author jc |
16
|
|
|
* @property \Ubiquity\db\Database $db |
17
|
|
|
*/ |
18
|
|
|
trait DAORelationsTrait { |
19
|
|
|
|
20
|
|
|
abstract protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL); |
21
|
|
|
|
22
|
62 |
|
public static function _getIncludedForStep($included) { |
23
|
62 |
|
if (\is_bool ( $included )) { |
24
|
62 |
|
return $included; |
25
|
|
|
} |
26
|
7 |
|
$ret = [ ]; |
27
|
7 |
|
if (\is_array ( $included )) { |
28
|
7 |
|
foreach ( $included as &$includedMember ) { |
29
|
7 |
|
if (\is_array ( $includedMember )) { |
30
|
|
|
foreach ( $includedMember as $iMember ) { |
31
|
|
|
self::parseEncludeMember ( $ret, $iMember ); |
32
|
|
|
} |
33
|
|
|
} else { |
34
|
7 |
|
self::parseEncludeMember ( $ret, $includedMember ); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
7 |
|
return $ret; |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
private static function parseEncludeMember(&$ret, $includedMember): void { |
42
|
7 |
|
$array = \explode ( '.', $includedMember ); |
43
|
7 |
|
$member = \array_shift ( $array ); |
44
|
7 |
|
if (\sizeof ( $array ) > 0) { |
45
|
|
|
$newValue = \implode ( '.', $array ); |
46
|
|
|
if ($newValue === '*') { |
47
|
|
|
$newValue = true; |
48
|
|
|
} |
49
|
|
|
if (isset ( $ret [$member] )) { |
50
|
|
|
if (! \is_array ( $ret [$member] )) { |
51
|
|
|
$ret [$member] = [ $ret [$member] ]; |
52
|
|
|
} |
53
|
|
|
$ret [$member] [] = $newValue; |
54
|
|
|
} else { |
55
|
|
|
$ret [$member] = $newValue; |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
7 |
|
if (isset ( $member ) && '' != $member) { |
59
|
7 |
|
$ret [$member] = false; |
60
|
|
|
} else { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
} |
64
|
7 |
|
} |
65
|
|
|
|
66
|
3 |
|
private static function getInvertedJoinColumns($included, &$invertedJoinColumns): void { |
67
|
3 |
|
foreach ( $invertedJoinColumns as $column => &$annot ) { |
68
|
3 |
|
$member = $annot ['member']; |
69
|
3 |
|
if (isset ( $included [$member] ) === false) { |
70
|
3 |
|
unset ( $invertedJoinColumns [$column] ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
|
|
|
75
|
7 |
|
private static function getToManyFields($included, &$toManyFields): void { |
76
|
7 |
|
foreach ( $toManyFields as $member => $annotNotUsed ) { |
77
|
7 |
|
if (isset ( $included [$member] ) === false) { |
78
|
7 |
|
unset ( $toManyFields [$member] ); |
79
|
|
|
} |
80
|
|
|
} |
81
|
7 |
|
} |
82
|
|
|
|
83
|
45 |
|
public static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields): void { |
84
|
45 |
|
if (isset ( $metaDatas ['#invertedJoinColumn'] )) { |
85
|
26 |
|
$invertedJoinColumns = $metaDatas ['#invertedJoinColumn']; |
86
|
|
|
} |
87
|
45 |
|
if (isset ( $metaDatas ['#oneToMany'] )) { |
88
|
40 |
|
$oneToManyFields = $metaDatas ['#oneToMany']; |
89
|
|
|
} |
90
|
45 |
|
if (isset ( $metaDatas ['#manyToMany'] )) { |
91
|
23 |
|
$manyToManyFields = $metaDatas ['#manyToMany']; |
92
|
|
|
} |
93
|
45 |
|
if (\is_array ( $included )) { |
94
|
7 |
|
if (isset ( $invertedJoinColumns )) { |
95
|
3 |
|
self::getInvertedJoinColumns ( $included, $invertedJoinColumns ); |
96
|
|
|
} |
97
|
7 |
|
if (isset ( $oneToManyFields )) { |
98
|
7 |
|
self::getToManyFields ( $included, $oneToManyFields ); |
99
|
|
|
} |
100
|
7 |
|
if (isset ( $manyToManyFields )) { |
101
|
3 |
|
self::getToManyFields ( $included, $manyToManyFields ); |
102
|
|
|
} |
103
|
|
|
} |
104
|
45 |
|
} |
105
|
|
|
|
106
|
|
|
private static function getManyToManyFromArray($instance, $array, $class, $parser): array { |
107
|
|
|
$ret = [ ]; |
108
|
|
|
$continue = true; |
109
|
|
|
$accessorToMember = 'get' . \ucfirst ( $parser->getInversedBy () ); |
110
|
|
|
$myPkAccessor = 'get' . \ucfirst ( $parser->getMyPk () ); |
111
|
|
|
$pk = self::getFirstKeyValue_ ( $instance ); |
112
|
|
|
|
113
|
|
|
if (sizeof ( $array ) > 0) { |
114
|
|
|
$continue = \method_exists ( current ( $array ), $accessorToMember ); |
115
|
|
|
} |
116
|
|
|
if ($continue) { |
117
|
|
|
foreach ( $array as $targetEntityInstance ) { |
118
|
|
|
$instances = $targetEntityInstance->$accessorToMember (); |
119
|
|
|
if (is_array ( $instances )) { |
120
|
|
|
foreach ( $instances as $inst ) { |
121
|
|
|
if ($inst->$myPkAccessor () == $pk) |
122
|
|
|
\array_push ( $ret, $targetEntityInstance ); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
Logger::warn ( 'DAO', "L'accesseur au membre " . $parser->getInversedBy () . ' est manquant pour ' . $parser->getTargetEntity (), 'ManyToMany' ); |
128
|
|
|
} |
129
|
|
|
return $ret; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Loads member associated with $instance by a ManyToOne relationship |
134
|
|
|
* |
135
|
|
|
* @param object|array $instance The instance object or an array with [classname,id] |
136
|
|
|
* @param string $member The member to load |
137
|
|
|
* @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"] |
138
|
|
|
* @param boolean|null $useCache |
139
|
|
|
*/ |
140
|
5 |
|
public static function getManyToOne($instance, $member, $included = false, $useCache = NULL): ?object { |
141
|
5 |
|
$classname = self::getClass_ ( $instance ); |
142
|
5 |
|
if (\is_array ( $instance )) { |
143
|
1 |
|
$instance = self::getById ( $classname, $instance [1], false, $useCache ); |
144
|
|
|
} |
145
|
5 |
|
$fieldAnnot = OrmUtils::getMemberJoinColumns ( $classname, $member ); |
146
|
5 |
|
if ($fieldAnnot !== null) { |
147
|
5 |
|
$annotationArray = $fieldAnnot [1]; |
148
|
5 |
|
$member = $annotationArray ['member']; |
149
|
5 |
|
$value = Reflexion::getMemberValue ( $instance, $member ); |
150
|
5 |
|
$key = OrmUtils::getFirstKey ( $annotationArray ['className'] ); |
151
|
5 |
|
$kv = array ($key => $value ); |
152
|
5 |
|
$obj = self::getById ( $annotationArray ['className'], $kv, $included, $useCache ); |
153
|
5 |
|
if ($obj !== null) { |
154
|
5 |
|
Logger::info ( 'DAO', 'Loading the member ' . $member . ' for the object ' . $classname, 'getManyToOne' ); |
155
|
5 |
|
$accesseur = 'set' . ucfirst ( $member ); |
156
|
5 |
|
if (\is_object ( $instance ) && \method_exists ( $instance, $accesseur )) { |
157
|
5 |
|
$instance->$accesseur ( $obj ); |
158
|
5 |
|
$instance->_rest [$member] = $obj->_rest; |
159
|
|
|
} |
160
|
5 |
|
return $obj; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Assign / load the child records in the $member member of $instance. |
168
|
|
|
* |
169
|
|
|
* @param object|array $instance The instance object or an array with [classname,id] |
170
|
|
|
* @param string $member Member on which a oneToMany annotation must be present |
171
|
|
|
* @param boolean|array $included if true, loads associate members with associations, if array, example : ['client.*','commands'] |
172
|
|
|
* @param boolean $useCache |
173
|
|
|
* @param array $annot used internally |
174
|
|
|
*/ |
175
|
7 |
|
public static function getOneToMany($instance, $member, $included = true, $useCache = NULL, $annot = null): array { |
176
|
7 |
|
$ret = array (); |
177
|
7 |
|
$class = self::getClass_ ( $instance ); |
178
|
7 |
|
if (! isset ( $annot )) { |
179
|
7 |
|
$annot = OrmUtils::getAnnotationInfoMember ( $class, '#oneToMany', $member ); |
180
|
|
|
} |
181
|
7 |
|
if ($annot !== false) { |
182
|
7 |
|
$fkAnnot = OrmUtils::getAnnotationInfoMember ( $annot ['className'], '#joinColumn', $annot ['mappedBy'] ); |
183
|
7 |
|
if ($fkAnnot !== false) { |
184
|
7 |
|
$fkv = self::getFirstKeyValue_ ( $instance ); |
185
|
7 |
|
$db = self::getDb ( $annot ['className'] ); |
186
|
7 |
|
$ret = self::_getAll ( $db, $annot ['className'], ConditionParser::simple ( $db->quote . $fkAnnot ['name'] . $db->quote . '= ?', $fkv ), $included, $useCache ); |
187
|
7 |
|
if (is_object ( $instance ) && $modifier = self::getAccessor ( $member, $instance, 'getOneToMany' )) { |
188
|
6 |
|
self::setToMember ( $member, $instance, $ret, $modifier ); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
7 |
|
return $ret; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Assigns / loads the child records in the $member member of $instance. |
197
|
|
|
* If $array is null, the records are loaded from the database |
198
|
|
|
* |
199
|
|
|
* @param object|array $instance The instance object or an array with [classname,id] |
200
|
|
|
* @param string $member Member on which a ManyToMany annotation must be present |
201
|
|
|
* @param boolean|array $included if true, loads associate members with associations, if array, example : ['client.*','commands'] |
202
|
|
|
* @param array $array optional parameter containing the list of possible child records |
203
|
|
|
* @param boolean $useCache |
204
|
|
|
*/ |
205
|
4 |
|
public static function getManyToMany($instance, $member, $included = false, $array = null, $useCache = NULL): array { |
206
|
4 |
|
$ret = [ ]; |
207
|
4 |
|
$class = self::getClass_ ( $instance ); |
208
|
4 |
|
$parser = new ManyToManyParser ( $class, $member ); |
209
|
4 |
|
if ($parser->init ()) { |
210
|
4 |
|
if (\is_null ( $array )) { |
211
|
4 |
|
$pk = self::getFirstKeyValue_ ( $instance ); |
212
|
4 |
|
$quote = SqlUtils::$quote; |
213
|
4 |
|
$condition = ' INNER JOIN ' . $quote . $parser->getJoinTable () . $quote . ' on ' . $quote . $parser->getJoinTable () . $quote . '.' . $quote . $parser->getFkField () . $quote . '=' . $quote . $parser->getTargetEntityTable () . $quote . '.' . $quote . $parser->getPk () . $quote . ' WHERE ' . $quote . $parser->getJoinTable () . $quote . '.' . $quote . $parser->getMyFkField () . $quote . '= ?'; |
214
|
4 |
|
$targetEntityClass = $parser->getTargetEntityClass (); |
215
|
4 |
|
$ret = self::_getAll ( self::getDb ( $targetEntityClass ), $targetEntityClass, ConditionParser::simple ( $condition, $pk ), $included, $useCache ); |
216
|
|
|
} else { |
217
|
|
|
$ret = self::getManyToManyFromArray ( $instance, $array, $class, $parser ); |
218
|
|
|
} |
219
|
4 |
|
if (\is_object ( $instance ) && $modifier = self::getAccessor ( $member, $instance, 'getManyToMany' )) { |
220
|
3 |
|
self::setToMember ( $member, $instance, $ret, $modifier ); |
221
|
|
|
} |
222
|
|
|
} |
223
|
4 |
|
return $ret; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* |
228
|
|
|
* @param object $instance |
229
|
|
|
* @param array $array |
230
|
|
|
* @param boolean $useCache |
231
|
|
|
*/ |
232
|
|
|
public static function affectsManyToManys($instance, $array = NULL, $useCache = NULL) { |
233
|
|
|
$metaDatas = OrmUtils::getModelMetadata ( \get_class ( $instance ) ); |
234
|
|
|
$manyToManyFields = $metaDatas ['#manyToMany']; |
235
|
|
|
if (\sizeof ( $manyToManyFields ) > 0) { |
236
|
|
|
foreach ( $manyToManyFields as $member ) { |
237
|
|
|
self::getManyToMany ( $instance, $member, false, $array, $useCache ); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|