|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\parser; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\OrmUtils; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* ManyToManyParser |
|
9
|
|
|
* @author jc |
|
10
|
|
|
* @version 1.1.0.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class ManyToManyParser { |
|
13
|
|
|
private $table; |
|
14
|
|
|
private $member; |
|
15
|
|
|
private $joinTable; |
|
16
|
|
|
private $myFkField; |
|
17
|
|
|
private $fkField; |
|
18
|
|
|
private $targetEntity; |
|
19
|
|
|
private $targetEntityClass; |
|
20
|
|
|
private $targetEntityTable; |
|
21
|
|
|
private $myPk; |
|
22
|
|
|
private $inversedBy; |
|
23
|
|
|
private $pk; |
|
24
|
|
|
private $instance; |
|
25
|
|
|
private $whereValues; |
|
26
|
|
|
|
|
27
|
5 |
|
public function __construct($instance, $member=null) { |
|
28
|
5 |
|
$this->instance=$instance; |
|
29
|
5 |
|
$this->member=$member; |
|
30
|
5 |
|
$this->whereValues=[]; |
|
31
|
5 |
|
} |
|
32
|
|
|
|
|
33
|
5 |
|
public function init($annot=false) { |
|
34
|
5 |
|
$member=$this->member; |
|
35
|
5 |
|
$class=$this->instance; |
|
36
|
5 |
|
if(\is_object($class)){ |
|
37
|
5 |
|
$class=get_class($class); |
|
38
|
|
|
} |
|
39
|
5 |
|
if($annot===false){ |
|
40
|
1 |
|
$annot=OrmUtils::getAnnotationInfoMember($class, "#manyToMany", $member); |
|
41
|
|
|
} |
|
42
|
5 |
|
if ($annot !== false) { |
|
43
|
5 |
|
$this->_init($class,$annot); |
|
44
|
5 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
private function _init($class,$annot){ |
|
50
|
5 |
|
$this->table=OrmUtils::getTableName($class); |
|
51
|
5 |
|
$this->targetEntity=$annot["targetEntity"]; |
|
52
|
5 |
|
$this->inversedBy=strtolower($this->targetEntity) . "s"; |
|
53
|
5 |
|
if (!is_null($annot["inversedBy"])) |
|
54
|
5 |
|
$this->inversedBy=$annot["inversedBy"]; |
|
55
|
5 |
|
$this->targetEntityClass=get_class(new $this->targetEntity()); |
|
56
|
|
|
|
|
57
|
5 |
|
$annotJoinTable=OrmUtils::getAnnotationInfoMember($class, "#joinTable", $this->member); |
|
58
|
5 |
|
$this->joinTable=$annotJoinTable["name"]; |
|
59
|
|
|
|
|
60
|
5 |
|
$this->myFkField=OrmUtils::getDefaultFk($class); |
|
61
|
5 |
|
$this->myPk=OrmUtils::getFirstKey($class); |
|
62
|
5 |
|
if(isset($annotJoinTable["joinColumns"])){ |
|
63
|
|
|
$joinColumnsAnnot=$annotJoinTable["joinColumns"]; |
|
64
|
|
|
if (!is_null($joinColumnsAnnot)) { |
|
65
|
|
|
$this->myFkField=$joinColumnsAnnot["name"]; |
|
66
|
|
|
$this->myPk=$joinColumnsAnnot["referencedColumnName"]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
5 |
|
$this->targetEntityTable=OrmUtils::getTableName($this->targetEntity); |
|
70
|
5 |
|
$this->fkField=OrmUtils::getDefaultFk($this->targetEntityClass); |
|
71
|
5 |
|
$this->pk=OrmUtils::getFirstKey($this->targetEntityClass); |
|
72
|
5 |
|
if(isset($annotJoinTable["inverseJoinColumns"])){ |
|
73
|
|
|
$inverseJoinColumnsAnnot=$annotJoinTable["inverseJoinColumns"]; |
|
74
|
|
|
if (!is_null($inverseJoinColumnsAnnot)) { |
|
75
|
|
|
$this->fkField=$inverseJoinColumnsAnnot["name"]; |
|
76
|
|
|
$this->pk=$inverseJoinColumnsAnnot["referencedColumnName"]; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
5 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getMember() { |
|
82
|
|
|
return $this->member; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setMember($member) { |
|
86
|
|
|
$this->member=$member; |
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function getJoinTable() { |
|
91
|
1 |
|
return $this->joinTable; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function setJoinTable($joinTable) { |
|
95
|
|
|
$this->joinTable=$joinTable; |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public function getMyFkField() { |
|
100
|
1 |
|
return $this->myFkField; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function setMyFkField($myFkField) { |
|
104
|
|
|
$this->myFkField=$myFkField; |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
public function getFkField() { |
|
109
|
1 |
|
return $this->fkField; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function setFkField($fkField) { |
|
113
|
|
|
$this->fkField=$fkField; |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getTargetEntity() { |
|
118
|
|
|
return $this->targetEntity; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function setTargetEntity($targetEntity) { |
|
122
|
|
|
$this->targetEntity=$targetEntity; |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
public function getTargetEntityClass() { |
|
127
|
1 |
|
return $this->targetEntityClass; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function setTargetEntityClass($targetEntityClass) { |
|
131
|
|
|
$this->targetEntityClass=$targetEntityClass; |
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
5 |
|
public function getTargetEntityTable() { |
|
136
|
5 |
|
return $this->targetEntityTable; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function setTargetEntityTable($targetEntityTable) { |
|
140
|
|
|
$this->targetEntityTable=$targetEntityTable; |
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
5 |
|
public function getMyPk() { |
|
145
|
5 |
|
return $this->myPk; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function setMyPk($myPk) { |
|
149
|
|
|
$this->myPk=$myPk; |
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
5 |
|
public function getPk() { |
|
154
|
5 |
|
return $this->pk; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function setPk($pk) { |
|
158
|
|
|
$this->pk=$pk; |
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function getInversedBy() { |
|
163
|
|
|
return $this->inversedBy; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function setInversedBy($inversedBy) { |
|
167
|
|
|
$this->inversedBy=$inversedBy; |
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function getInstance() { |
|
172
|
|
|
return $this->instance; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function setInstance($instance) { |
|
176
|
|
|
$this->instance=$instance; |
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function getSQL($alias="",$aliases=null){ |
|
181
|
|
|
if($alias!==""){ |
|
182
|
|
|
$targetEntityTable=$alias; |
|
183
|
|
|
$alias="`".$alias."`"; |
|
184
|
|
|
}else{ |
|
185
|
|
|
$targetEntityTable=$this->targetEntityTable; |
|
186
|
|
|
} |
|
187
|
|
|
$jtAlias=uniqid($this->joinTable); |
|
188
|
|
|
$table=$this->table; |
|
189
|
|
|
if(is_array($aliases)){ |
|
190
|
|
|
if(isset($aliases[$this->table])) |
|
191
|
|
|
$table=$aliases[$this->table]; |
|
192
|
|
|
} |
|
193
|
|
|
return " INNER JOIN `" . $this->joinTable . "` `{$jtAlias}` on `".$jtAlias."`.`".$this->myFkField."`=`".$table."`.`".$this->myPk."`". |
|
194
|
|
|
" INNER JOIN `" . $this->targetEntityTable . "` {$alias} on `".$jtAlias."`.`".$this->fkField."`=`".$targetEntityTable."`.`".$this->pk."`"; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
4 |
|
public function getConcatSQL(){ |
|
198
|
4 |
|
return "SELECT `".$this->myFkField."` as '_field' ,GROUP_CONCAT(`".$this->fkField."` SEPARATOR ',') as '_concat' FROM `".$this->joinTable."` {condition} GROUP BY 1"; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
4 |
|
public function getParserWhereMask($mask="'{value}'"){ |
|
202
|
4 |
|
return "`".$this->getTargetEntityTable()."`.`". $this->getPk() . "`=".$mask; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
2 |
|
private function getParserConcatWhereMask($mask="'{value}'"){ |
|
206
|
2 |
|
return "`".$this->myFkField. "`=".$mask; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
3 |
|
private function getParserConcatWhereInMask($mask="'{values}'"){ |
|
210
|
3 |
|
return " INNER JOIN (".$mask.") as _tmp ON `".$this->myFkField. "`=_tmp._id"; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
4 |
|
public function generateConcatSQL(){ |
|
215
|
4 |
|
$sql=$this->getConcatSQL(); |
|
216
|
4 |
|
$where=""; |
|
217
|
4 |
|
if(($size=sizeof($this->whereValues))>0){ |
|
218
|
4 |
|
if($size>3){ |
|
219
|
3 |
|
$res=array_fill(0, $size, "?"); |
|
220
|
3 |
|
$res[0]="SELECT ? as _id"; |
|
221
|
3 |
|
$where=$this->getParserConcatWhereInMask(implode(" UNION ALL SELECT ", $res)); |
|
222
|
|
|
}else{ |
|
223
|
2 |
|
$mask=$this->getParserConcatWhereMask(" ?"); |
|
224
|
2 |
|
$res=array_fill(0, $size, $mask); |
|
225
|
2 |
|
$where="WHERE ".implode(" OR ", $res); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
4 |
|
return str_replace("{condition}", $where, $sql); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
4 |
|
public function addValue($value){ |
|
232
|
4 |
|
$this->whereValues[$value]=true; |
|
233
|
4 |
|
} |
|
234
|
|
|
/** |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
4 |
|
public function getWhereValues() { |
|
238
|
4 |
|
return array_keys($this->whereValues); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|