Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
OrmUtilsRelationsTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\orm\traits;
4
6
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 public static function getFieldsInRelations($class) {
51 $result = [ ];
52
53 if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) {
54 $result = \array_merge ( $result, $manyToOne );
55 }
56 if ($oneToMany = self::getAnnotationInfo ( $class, '#oneToMany' )) {
57 $result = \array_merge ( $result, \array_keys ( $oneToMany ) );
58 }
59 if ($manyToMany = self::getAnnotationInfo ( $class, '#manyToMany' )) {
60 $result = \array_merge ( $result, \array_keys ( $manyToMany ) );
61 }
62 return $result;
63 }
64
65 public static function getRelationInfos($class) {
66 $result = [ ];
67 $joinColumns = self::getAnnotationInfo ( $class, '#joinColumn' );
68 $invertedJoinColumns = self::getAnnotationInfo ( $class, '#invertedJoinColumn' );
69 if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) {
70 foreach ( $manyToOne as $oneField ) {
71 $field = $joinColumns [$oneField] ['name'];
72 $result [$field] = $invertedJoinColumns [$field];
73 $result [$oneField] = $invertedJoinColumns [$field];
74 }
75 }
76 if ($oneToMany = self::getAnnotationInfo ( $class, '#oneToMany' )) {
77 $result = \array_merge ( $result, $oneToMany );
78 }
79 if ($manyToMany = self::getAnnotationInfo ( $class, '#manyToMany' )) {
80 $result = \array_merge ( $result, $manyToMany );
81 }
82 return $result;
83 }
84
85 public static function getFieldsInRelations_($class) {
86 return self::getFieldsInRelationsForUpdate_ ( $class ) ['relations'];
87 }
88
89 public static function getFieldsInRelationsForUpdate_($class) {
90 $result = [ ];
91 if ($manyToOne = self::getAnnotationInfo ( $class, '#manyToOne' )) {
92 foreach ( $manyToOne as $member ) {
93 $joinColumn = self::getAnnotationInfoMember ( $class, '#joinColumn', $member );
94 $result [$joinColumn ['name']] = $member;
95 }
96 }
97 if ($manyToMany = self::getAnnotationInfo ( $class, "#manyToMany" )) {
98 $manyToMany = array_keys ( $manyToMany );
99 foreach ( $manyToMany as $member ) {
100 $result [$member . 'Ids'] = $member;
101 }
102 }
103 if ($oneToMany = self::getAnnotationInfo ( $class, '#oneToMany' )) {
104 $oneToMany = array_keys ( $oneToMany );
105 foreach ( $oneToMany as $member ) {
106 $result [$member . 'Ids'] = $member;
107 }
108 }
109 return [ 'relations' => $result,'manyToOne' => $manyToOne,'manyToMany' => $manyToMany,'oneToMany' => $oneToMany ];
110 }
111
112 public static function getAnnotFieldsInRelations($class) {
113 $result = [ ];
114 if ($manyToOnes = self::getAnnotationInfo ( $class, '#manyToOne' )) {
115 $joinColumns = self::getAnnotationInfo ( $class, '#joinColumn' );
116 foreach ( $manyToOnes as $manyToOne ) {
117 if (isset ( $joinColumns [$manyToOne] )) {
118 $result [$manyToOne] = [ 'type' => 'manyToOne','value' => $joinColumns [$manyToOne] ];
119 }
120 }
121 }
122 if ($oneToManys = self::getAnnotationInfo ( $class, '#oneToMany' )) {
123 foreach ( $oneToManys as $field => $oneToMany ) {
124 $result [$field] = [ 'type' => 'oneToMany','value' => $oneToMany ];
125 }
126 }
127 if ($manyToManys = self::getAnnotationInfo ( $class, "#manyToMany" )) {
128 foreach ( $manyToManys as $field => $manyToMany ) {
129 $result [$field] = [ 'type' => 'manyToMany','value' => $manyToMany ];
130 }
131 }
132 return $result;
133 }
134
135 public static function getUJoinSQL($db, $model, $arrayAnnot, $field, &$aliases, $quote) {
136 $type = $arrayAnnot ['type'];
137 $annot = $arrayAnnot ['value'];
138 $table = self::getTableName ( $model );
139 $tableAlias = (isset ( $aliases [$table] )) ? $aliases [$table] : $table;
140 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 } 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 $parser = new ManyToManyParser ( $db, $model, $field );
157 $parser->init ( $annot );
158 $fkTable = $parser->getTargetEntityTable ();
159 $fkClass = $parser->getTargetEntityClass ();
160 $alias = self::getJoinAlias ( $table, $fkTable );
161 $result = $parser->getSQL ( $alias, $aliases );
162 }
163
164 if (array_search ( $alias, $aliases ) !== false) {
165 $result = "";
166 }
167 $aliases [$fkTable] = $alias;
168 return [ 'class' => $fkClass,'table' => $fkTable,'sql' => $result,'alias' => $alias ];
169 }
170
171 private static function getJoinAlias($table, $fkTable) {
172 return \uniqid ( $fkTable . '_' . $table [0] );
173 }
174
175 public static function getOneToManyFields($class) {
176 return self::getAnnotationInfo ( $class, '#oneToMany' );
177 }
178
179 public static function getRemoveCascadeFields($class,$keyAnnotation='#oneToMany') {
180 $infos= self::getAnnotationInfo ( $class, $keyAnnotation);
181 $res=[];
182 if($infos!==false){
183 foreach ($infos as $f=>$annot){
184 if(\array_search('remove',$annot['cascade']??[])!==false){
185 $res[]=$f;
186 }
187 }
188 }
189 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 public static function getDefaultFk($classname) {
204 return 'id' . \ucfirst ( self::getTableName ( $classname ) );
205 }
206
207 public static function getMemberJoinColumns($instance, $member, $metaDatas = NULL) {
208 if (! isset ( $metaDatas )) {
209 if (\is_object ( $instance )) {
210 $class = \get_class ( $instance );
211 } else {
212 $class = $instance;
213 }
214 $metaDatas = self::getModelMetadata ( $class );
215 }
216 $invertedJoinColumns = $metaDatas ['#invertedJoinColumn'];
217 foreach ( $invertedJoinColumns as $field => $invertedJoinColumn ) {
218 if ($invertedJoinColumn ['member'] === $member) {
219 return [ $field,$invertedJoinColumn ];
220 }
221 }
222 return null;
223 }
224
230 public static function getManyToOneMembersAndValues($instance) {
231 $ret = array ();
232 $class = \get_class ( $instance );
233 $members = self::getAnnotationInfo ( $class, '#manyToOne' );
234 if ($members !== false) {
235 foreach ( $members as $member ) {
236 $memberAccessor = 'get' . ucfirst ( $member );
237 if (\method_exists ( $instance, $memberAccessor )) {
238 $memberInstance = $instance->$memberAccessor ();
239 if (isset ( $memberInstance ) ){
240 if(\is_object ( $memberInstance )) {
241 $keyValues = self::getKeyFieldsAndValues($memberInstance);
242 if (\count($keyValues) > 0) {
243 $fkName = self::getJoinColumnName($class, $member);
244 $ret [$fkName] = \current($keyValues);
245 }
246 }else{
247 $fkName = self::getJoinColumnName($class, $member);
248 $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 return $ret;
258 }
259
260 public static function getJoinColumnName($class, $member) {
261 $annot = self::getAnnotationInfoMember ( $class, '#joinColumn', $member );
262 if ($annot !== false) {
263 $fkName = $annot ['name'];
264 } else {
265 $fkName = 'id' . \ucfirst ( self::getTableName ( \ucfirst ( $member ) ) );
266 }
267 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
static getAnnotationInfo($class, $keyAnnotation)
static getRemoveCascadeFields($class, $keyAnnotation='#oneToMany')
static getMemberJoinColumns($instance, $member, $metaDatas=NULL)
static getUJoinSQL($db, $model, $arrayAnnot, $field, &$aliases, $quote)
static getAnnotationInfoMember($class, $keyAnnotation, $member)
Ubiquity\orm\traits This class is part of Ubiquity.