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