1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
4
|
|
|
|
5
|
|
|
use Ubiquity\orm\parser\ManyToManyParser; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait for DAO relations. |
9
|
|
|
* Ubiquity\orm\traits$OrmUtilsRelationsTrait |
10
|
|
|
* This class is part of Ubiquity |
11
|
|
|
* |
12
|
|
|
* @author jcheron <[email protected]> |
13
|
|
|
* @version 1.0.7 |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
trait OrmUtilsRelationsTrait { |
17
|
|
|
|
18
|
|
|
abstract public static function getAnnotationInfoMember($class, $keyAnnotation, $member); |
19
|
|
|
|
20
|
|
|
abstract public static function getAnnotationInfo($class, $keyAnnotation); |
21
|
|
|
|
22
|
|
|
abstract public static function getTableName($class); |
23
|
|
|
|
24
|
|
|
abstract public static function getFirstKey($class); |
25
|
|
|
|
26
|
|
|
abstract public static function getModelMetadata($className); |
27
|
|
|
|
28
|
|
|
abstract public static function getKeyFieldsAndValues($instance); |
29
|
|
|
|
30
|
|
|
public static function getJoinTables($class) { |
31
|
|
|
$result = [ ]; |
32
|
|
|
|
33
|
|
|
if (isset ( self::getModelMetadata ( $class ) ['#joinTable'] )) { |
34
|
|
|
$jts = self::getModelMetadata ( $class ) ['#joinTable']; |
35
|
|
|
foreach ( $jts as $jt ) { |
36
|
|
|
$result [] = $jt ['name']; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function getAllJoinTables($models) { |
43
|
|
|
$result = [ ]; |
44
|
|
|
foreach ( $models as $model ) { |
45
|
|
|
$result = \array_merge ( $result, self::getJoinTables ( $model ) ); |
46
|
|
|
} |
47
|
|
|
return $result; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public static function getFieldsInRelations($class) { |
51
|
4 |
|
$result = [ ]; |
52
|
|
|
|
53
|
4 |
|
if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) { |
54
|
1 |
|
$result = \array_merge ( $result, $manyToOne ); |
55
|
|
|
} |
56
|
4 |
|
if ($oneToMany = self::getAnnotationInfo ( $class, '#oneToMany' )) { |
57
|
4 |
|
$result = \array_merge ( $result, \array_keys ( $oneToMany ) ); |
58
|
|
|
} |
59
|
4 |
|
if ($manyToMany = self::getAnnotationInfo ( $class, '#manyToMany' )) { |
60
|
1 |
|
$result = \array_merge ( $result, \array_keys ( $manyToMany ) ); |
61
|
|
|
} |
62
|
4 |
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public static function getRelationInfos($class) { |
66
|
3 |
|
$result = [ ]; |
67
|
3 |
|
$joinColumns = self::getAnnotationInfo ( $class, '#joinColumn' ); |
68
|
3 |
|
$invertedJoinColumns = self::getAnnotationInfo ( $class, '#invertedJoinColumn' ); |
69
|
3 |
|
if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) { |
70
|
2 |
|
foreach ( $manyToOne as $oneField ) { |
71
|
2 |
|
$field = $joinColumns [$oneField] ['name']; |
72
|
2 |
|
$result [$field] = $invertedJoinColumns [$field]; |
73
|
2 |
|
$result [$oneField] = $invertedJoinColumns [$field]; |
74
|
|
|
} |
75
|
|
|
} |
76
|
3 |
|
if ($oneToMany = self::getAnnotationInfo ( $class, '#oneToMany' )) { |
77
|
3 |
|
$result = \array_merge ( $result, $oneToMany ); |
78
|
|
|
} |
79
|
3 |
|
if ($manyToMany = self::getAnnotationInfo ( $class, '#manyToMany' )) { |
80
|
2 |
|
$result = \array_merge ( $result, $manyToMany ); |
81
|
|
|
} |
82
|
3 |
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
public static function getFieldsInRelations_($class) { |
86
|
5 |
|
return self::getFieldsInRelationsForUpdate_ ( $class ) ['relations']; |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
public static function getFieldsInRelationsForUpdate_($class) { |
90
|
6 |
|
$result = [ ]; |
91
|
6 |
|
if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) { |
92
|
2 |
|
foreach ( $manyToOne as $member ) { |
93
|
2 |
|
$joinColumn = self::getAnnotationInfoMember ( $class, '#joinColumn', $member ); |
94
|
2 |
|
$result [$joinColumn ['name']] = $member; |
95
|
|
|
} |
96
|
|
|
} |
97
|
6 |
|
if ($manyToMany = self::getAnnotationInfo ( $class, "#manyToMany" )) { |
98
|
1 |
|
$manyToMany = array_keys ( $manyToMany ); |
99
|
1 |
|
foreach ( $manyToMany as $member ) { |
100
|
1 |
|
$result [$member . 'Ids'] = $member; |
101
|
|
|
} |
102
|
|
|
} |
103
|
6 |
|
if ($oneToMany = self::getAnnotationInfo ( $class, '#oneToMany' )) { |
104
|
4 |
|
$oneToMany = array_keys ( $oneToMany ); |
105
|
4 |
|
foreach ( $oneToMany as $member ) { |
106
|
4 |
|
$result [$member . 'Ids'] = $member; |
107
|
|
|
} |
108
|
|
|
} |
109
|
6 |
|
return [ 'relations' => $result,'manyToOne' => $manyToOne,'manyToMany' => $manyToMany,'oneToMany' => $oneToMany ]; |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public static function getAnnotFieldsInRelations($class) { |
113
|
2 |
|
$result = [ ]; |
114
|
2 |
|
if ($manyToOnes = self::getAnnotationInfo ( $class, '#manyToOne' )) { |
115
|
1 |
|
$joinColumns = self::getAnnotationInfo ( $class, '#joinColumn' ); |
116
|
1 |
|
foreach ( $manyToOnes as $manyToOne ) { |
117
|
1 |
|
if (isset ( $joinColumns [$manyToOne] )) { |
118
|
1 |
|
$result [$manyToOne] = [ 'type' => 'manyToOne','value' => $joinColumns [$manyToOne] ]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
2 |
|
if ($oneToManys = self::getAnnotationInfo ( $class, '#oneToMany' )) { |
123
|
2 |
|
foreach ( $oneToManys as $field => $oneToMany ) { |
124
|
2 |
|
$result [$field] = [ 'type' => 'oneToMany','value' => $oneToMany ]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
2 |
|
if ($manyToManys = self::getAnnotationInfo ( $class, "#manyToMany" )) { |
128
|
1 |
|
foreach ( $manyToManys as $field => $manyToMany ) { |
129
|
1 |
|
$result [$field] = [ 'type' => 'manyToMany','value' => $manyToMany ]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
2 |
|
return $result; |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
public static function getUJoinSQL($db, $model, $arrayAnnot, $field, &$aliases, $quote) { |
136
|
4 |
|
$type = $arrayAnnot ['type']; |
137
|
4 |
|
$annot = $arrayAnnot ['value']; |
138
|
4 |
|
$table = self::getTableName ( $model ); |
139
|
4 |
|
$tableAlias = (isset ( $aliases [$table] )) ? $aliases [$table] : $table; |
140
|
4 |
|
if ($type === 'manyToOne') { |
141
|
|
|
$fkClass = $annot ['className']; |
142
|
|
|
$fkTable = self::getTableName ( $fkClass ); |
143
|
|
|
$fkField = $annot ['name']; |
144
|
|
|
$pkField = self::getFirstKey ( $fkClass ); |
145
|
|
|
$alias = self::getJoinAlias ( $table, $fkTable ); |
146
|
|
|
$result = "LEFT JOIN {$quote}{$fkTable}{$quote} {$quote}{$alias}{$quote} ON {$quote}{$tableAlias}{$quote}.{$quote}{$fkField}{$quote}={$quote}{$alias}{$quote}.{$quote}{$pkField}{$quote}"; |
147
|
4 |
|
} elseif ($type === 'oneToMany') { |
148
|
|
|
$fkClass = $annot ['className']; |
149
|
|
|
$fkAnnot = self::getAnnotationInfoMember ( $fkClass, '#joinColumn', $annot ['mappedBy'] ); |
150
|
|
|
$fkTable = self::getTableName ( $fkClass ); |
151
|
|
|
$fkField = $fkAnnot ['name']; |
152
|
|
|
$pkField = self::getFirstKey ( $model ); |
153
|
|
|
$alias = self::getJoinAlias ( $table, $fkTable ); |
154
|
|
|
$result = "LEFT JOIN {$quote}{$fkTable}{$quote} {$quote}{$alias}{$quote} ON {$quote}{$tableAlias}{$quote}.{$quote}{$pkField}{$quote}={$quote}{$alias}{$quote}.{$quote}{$fkField}{$quote}"; |
155
|
|
|
} else { |
156
|
4 |
|
$parser = new ManyToManyParser ( $db, $model, $field ); |
157
|
4 |
|
$parser->init ( $annot ); |
158
|
4 |
|
$fkTable = $parser->getTargetEntityTable (); |
159
|
4 |
|
$fkClass = $parser->getTargetEntityClass (); |
160
|
4 |
|
$alias = self::getJoinAlias ( $table, $fkTable ); |
161
|
4 |
|
$result = $parser->getSQL ( $alias, $aliases ); |
162
|
|
|
} |
163
|
|
|
|
164
|
4 |
|
if (array_search ( $alias, $aliases ) !== false) { |
165
|
|
|
$result = ""; |
166
|
|
|
} |
167
|
4 |
|
$aliases [$fkTable] = $alias; |
168
|
4 |
|
return [ 'class' => $fkClass,'table' => $fkTable,'sql' => $result,'alias' => $alias ]; |
169
|
|
|
} |
170
|
|
|
|
171
|
4 |
|
private static function getJoinAlias($table, $fkTable) { |
172
|
4 |
|
return uniqid ( $fkTable . '_' . $table [0] ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public static function getOneToManyFields($class) { |
176
|
|
|
return self::getAnnotationInfo ( $class, '#oneToMany' ); |
177
|
|
|
} |
178
|
|
|
|
179
|
13 |
|
public static function getRemoveCascadeFields($class,$keyAnnotation='#oneToMany') { |
180
|
13 |
|
$infos= self::getAnnotationInfo ( $class, $keyAnnotation); |
181
|
13 |
|
$res=[]; |
182
|
13 |
|
if($infos!==false){ |
183
|
13 |
|
foreach ($infos as $f=>$annot){ |
184
|
13 |
|
if(\array_search('remove',$annot['cascade']??[])!==false){ |
185
|
|
|
$res[]=$f; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
13 |
|
return $res; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public static function getManyToOneFields($class) { |
193
|
|
|
return self::getAnnotationInfo ( $class, '#manyToOne' ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public static function getManyToManyFields($class) { |
197
|
|
|
$result = self::getAnnotationInfo ( $class, '#manyToMany' ); |
198
|
|
|
if ($result !== false) |
199
|
|
|
return \array_keys ( $result ); |
200
|
|
|
return [ ]; |
201
|
|
|
} |
202
|
|
|
|
203
|
38 |
|
public static function getDefaultFk($classname) { |
204
|
38 |
|
return 'id' . \ucfirst ( self::getTableName ( $classname ) ); |
205
|
|
|
} |
206
|
|
|
|
207
|
6 |
|
public static function getMemberJoinColumns($instance, $member, $metaDatas = NULL) { |
208
|
6 |
|
if (! isset ( $metaDatas )) { |
209
|
5 |
|
if (is_object ( $instance )) { |
210
|
|
|
$class = get_class ( $instance ); |
211
|
|
|
} else { |
212
|
5 |
|
$class = $instance; |
213
|
|
|
} |
214
|
5 |
|
$metaDatas = self::getModelMetadata ( $class ); |
215
|
|
|
} |
216
|
6 |
|
$invertedJoinColumns = $metaDatas ['#invertedJoinColumn']; |
217
|
6 |
|
foreach ( $invertedJoinColumns as $field => $invertedJoinColumn ) { |
218
|
6 |
|
if ($invertedJoinColumn ['member'] === $member) { |
219
|
6 |
|
return [ $field,$invertedJoinColumn ]; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
return null; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* |
227
|
|
|
* @param object $instance |
228
|
|
|
* @return mixed[] |
229
|
|
|
*/ |
230
|
22 |
|
public static function getManyToOneMembersAndValues($instance) { |
231
|
22 |
|
$ret = array (); |
232
|
22 |
|
$class = \get_class ( $instance ); |
233
|
22 |
|
$members = self::getAnnotationInfo ( $class, '#manyToOne' ); |
234
|
22 |
|
if ($members !== false) { |
235
|
22 |
|
foreach ( $members as $member ) { |
236
|
8 |
|
$memberAccessor = 'get' . ucfirst ( $member ); |
237
|
8 |
|
if (\method_exists ( $instance, $memberAccessor )) { |
238
|
8 |
|
$memberInstance = $instance->$memberAccessor (); |
239
|
8 |
|
if (isset ( $memberInstance ) ){ |
240
|
8 |
|
if(\is_object ( $memberInstance )) { |
241
|
8 |
|
$keyValues = self::getKeyFieldsAndValues($memberInstance); |
242
|
8 |
|
if (\count($keyValues) > 0) { |
243
|
8 |
|
$fkName = self::getJoinColumnName($class, $member); |
244
|
8 |
|
$ret [$fkName] = \current($keyValues); |
245
|
|
|
} |
246
|
|
|
}else{ |
247
|
|
|
$fkName = self::getJoinColumnName($class, $member); |
248
|
8 |
|
$ret [$fkName] = $memberInstance; |
249
|
|
|
} |
250
|
|
|
} elseif (self::isNullable ( $class, $member )) { |
251
|
|
|
$fkName = self::getJoinColumnName ( $class, $member ); |
252
|
|
|
$ret [$fkName] = null; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
22 |
|
return $ret; |
258
|
|
|
} |
259
|
|
|
|
260
|
8 |
|
public static function getJoinColumnName($class, $member) { |
261
|
8 |
|
$annot = self::getAnnotationInfoMember ( $class, '#joinColumn', $member ); |
262
|
8 |
|
if ($annot !== false) { |
263
|
8 |
|
$fkName = $annot ['name']; |
264
|
|
|
} else { |
265
|
|
|
$fkName = 'id' . \ucfirst ( self::getTableName ( \ucfirst ( $member ) ) ); |
266
|
|
|
} |
267
|
8 |
|
return $fkName; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public static function isManyToMany($class):bool{ |
271
|
|
|
$metas=self::getModelMetadata ( $class ); |
272
|
|
|
$pks=$metas['#primaryKeys']; |
273
|
|
|
$manyToOnes=$metas['#manyToOne']; |
274
|
|
|
$manysCount=\count($manyToOnes); |
275
|
|
|
$counter=0; |
276
|
|
|
if($manysCount>1) { |
277
|
|
|
foreach ($manyToOnes as $manyToOne) { |
278
|
|
|
$len = \strlen($manyToOne); |
279
|
|
|
foreach ($pks as $k) { |
280
|
|
|
if (\substr($k, -$len) === \ucfirst($manyToOne)) { |
281
|
|
|
$counter++; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
return $counter>1; |
286
|
|
|
} |
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public static function getManyToManyFieldsDt($class,$manyClass) { |
291
|
|
|
$fields=self::getSerializableMembers($manyClass); |
292
|
|
|
$joinColumns=self::getModelMetadata($manyClass)['#joinColumn']; |
293
|
|
|
foreach ($joinColumns as $joinColumn){ |
294
|
|
|
if($joinColumn['className']===$class){ |
295
|
|
|
if($index=\array_search($joinColumn['name'],$fields)!==false){ |
296
|
|
|
unset($fields[$index]); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
return \array_values($fields); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|