|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\parser\ManyToManyParser; |
|
6
|
|
|
|
|
7
|
|
|
trait OrmUtilsRelationsTrait { |
|
8
|
|
|
|
|
9
|
|
|
abstract public static function getAnnotationInfoMember($class, $keyAnnotation, $member); |
|
10
|
|
|
abstract public static function getAnnotationInfo($class, $keyAnnotation); |
|
11
|
|
|
abstract public static function getTableName($class); |
|
12
|
|
|
abstract public static function getFirstKey($class); |
|
13
|
|
|
|
|
14
|
|
|
public static function getFieldsInRelations($class) { |
|
15
|
|
|
$result=[ ]; |
|
16
|
|
|
|
|
17
|
|
|
if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) { |
|
18
|
|
|
$result=\array_merge($result, $manyToOne); |
|
19
|
|
|
} |
|
20
|
|
|
if ($oneToMany=self::getAnnotationInfo($class, "#oneToMany")) { |
|
21
|
|
|
$result=\array_merge($result, \array_keys($oneToMany)); |
|
22
|
|
|
} |
|
23
|
|
|
if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) { |
|
24
|
|
|
$result=\array_merge($result, \array_keys($manyToMany)); |
|
25
|
|
|
} |
|
26
|
|
|
return $result; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function getFieldsInRelations_($class) { |
|
30
|
|
|
return self::getFieldsInRelationsForUpdate_($class)["relations"]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function getFieldsInRelationsForUpdate_($class) { |
|
34
|
|
|
$result=[ ]; |
|
35
|
|
View Code Duplication |
if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) { |
|
|
|
|
|
|
36
|
|
|
foreach ($manyToOne as $member){ |
|
37
|
|
|
$joinColumn = self::getAnnotationInfoMember ( $class, "#joinColumn", $member ); |
|
38
|
|
|
$result[$joinColumn["name"]]=$member; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
View Code Duplication |
if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) { |
|
|
|
|
|
|
42
|
|
|
$manyToMany=array_keys($manyToMany); |
|
43
|
|
|
foreach ($manyToMany as $member){ |
|
44
|
|
|
$result[$member . "Ids"]=$member; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
View Code Duplication |
if ($oneToMany=self::getAnnotationInfo($class, "#oneToMany")) { |
|
|
|
|
|
|
48
|
|
|
$oneToMany=array_keys($oneToMany); |
|
49
|
|
|
foreach ($oneToMany as $member){ |
|
50
|
|
|
$result[$member . "Ids"]=$member; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return ["relations"=>$result,"manyToOne"=>$manyToOne,"manyToMany"=>$manyToMany,"oneToMany"=>$oneToMany]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function getAnnotFieldsInRelations($class) { |
|
57
|
|
|
$result=[ ]; |
|
58
|
|
|
if ($manyToOnes=self::getAnnotationInfo($class, "#manyToOne")) { |
|
59
|
|
|
$joinColumns=self::getAnnotationInfo($class, "#joinColumn"); |
|
60
|
|
|
foreach ($manyToOnes as $manyToOne ){ |
|
61
|
|
|
if(isset($joinColumns[$manyToOne])){ |
|
62
|
|
|
$result[$manyToOne]=["type"=>"manyToOne","value"=>$joinColumns[$manyToOne]]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
View Code Duplication |
if ($oneToManys=self::getAnnotationInfo($class, "#oneToMany")) { |
|
|
|
|
|
|
67
|
|
|
foreach ($oneToManys as $field=>$oneToMany){ |
|
68
|
|
|
$result[$field]=["type"=>"oneToMany","value"=>$oneToMany]; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
View Code Duplication |
if ($manyToManys=self::getAnnotationInfo($class, "#manyToMany")) { |
|
|
|
|
|
|
72
|
|
|
foreach ($manyToManys as $field=>$manyToMany){ |
|
73
|
|
|
$result[$field]=["type"=>"manyToMany","value"=>$manyToMany]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static function getUJoinSQL($model,$arrayAnnot,$field,&$aliases){ |
|
80
|
|
|
$type=$arrayAnnot["type"];$annot=$arrayAnnot["value"]; |
|
81
|
|
|
$table=self::getTableName($model); |
|
82
|
|
|
$tableAlias=(isset($aliases[$table]))?$aliases[$table]:$table; |
|
83
|
|
|
if($type==="manyToOne"){ |
|
84
|
|
|
$fkClass=$annot["className"]; |
|
85
|
|
|
$fkTable=self::getTableName($fkClass); |
|
86
|
|
|
$fkField=$annot["name"]; |
|
87
|
|
|
$pkField=self::getFirstKey($fkClass); |
|
88
|
|
|
$alias=self::getJoinAlias($table, $fkTable); |
|
89
|
|
|
$result="INNER JOIN `{$fkTable}` `{$alias}` ON `{$tableAlias}`.`{$fkField}`=`{$alias}`.`{$pkField}`"; |
|
90
|
|
|
}elseif($type==="oneToMany"){ |
|
91
|
|
|
$fkClass=$annot["className"]; |
|
92
|
|
|
$fkAnnot=self::getAnnotationInfoMember($fkClass, "#joinColumn", $annot["mappedBy"]); |
|
93
|
|
|
$fkTable=self::getTableName($fkClass); |
|
94
|
|
|
$fkField=$fkAnnot["name"]; |
|
95
|
|
|
$pkField=self::getFirstKey($model); |
|
96
|
|
|
$alias=self::getJoinAlias($table, $fkTable); |
|
97
|
|
|
$result="INNER JOIN `{$fkTable}` `{$alias}` ON `{$tableAlias}`.`{$pkField}`=`{$alias}`.`{$fkField}`"; |
|
98
|
|
|
}else{ |
|
99
|
|
|
$parser=new ManyToManyParser($model,$field); |
|
100
|
|
|
$parser->init($annot); |
|
101
|
|
|
$fkTable=$parser->getTargetEntityTable(); |
|
102
|
|
|
$fkClass=$parser->getTargetEntityClass(); |
|
103
|
|
|
$alias=self::getJoinAlias($table, $fkTable); |
|
104
|
|
|
$result=$parser->getSQL($alias,$aliases); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if(array_search($alias, $aliases)!==false){ |
|
108
|
|
|
$result=""; |
|
109
|
|
|
} |
|
110
|
|
|
$aliases[$fkTable]=$alias; |
|
111
|
|
|
return ["class"=>$fkClass,"table"=>$fkTable,"sql"=>$result,"alias"=>$alias]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private static function getJoinAlias($table,$fkTable){ |
|
115
|
|
|
return uniqid($fkTable.'_'.$table[0]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public static function getOneToManyFields($class) { |
|
119
|
|
|
return self::getAnnotationInfo($class, "#oneToMany"); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public static function getManyToOneFields($class) { |
|
123
|
|
|
return self::getAnnotationInfo($class, "#manyToOne"); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public static function getManyToManyFields($class) { |
|
127
|
|
|
$result=self::getAnnotationInfo($class, "#manyToMany"); |
|
128
|
|
|
if($result!==false) |
|
129
|
|
|
return \array_keys($result); |
|
130
|
|
|
return []; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public static function getDefaultFk($classname) { |
|
134
|
|
|
return "id" . \ucfirst(self::getTableName($classname)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public static function getMemberJoinColumns($instance,$member,$metaDatas=NULL){ |
|
138
|
|
|
if(!isset($metaDatas)){ |
|
139
|
|
|
$class=get_class($instance); |
|
140
|
|
|
$metaDatas=self::getModelMetadata($class); |
|
141
|
|
|
} |
|
142
|
|
|
$invertedJoinColumns=$metaDatas["#invertedJoinColumn"]; |
|
143
|
|
|
foreach ($invertedJoinColumns as $field=>$invertedJoinColumn){ |
|
144
|
|
|
if($invertedJoinColumn["member"]===$member){ |
|
145
|
|
|
return [$field,$invertedJoinColumn]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.