|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bludata\Doctrine\ORM\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query; |
|
6
|
|
|
|
|
7
|
|
|
class QueryWorker |
|
8
|
|
|
{ |
|
9
|
|
|
const CUSTOM_FILTERS_KEY = 'custom'; |
|
|
|
|
|
|
10
|
|
|
const DEFAULT_TABLE_ALIAS = 't'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var Doctrine\ORM\EntityRepository |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $repository; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Doctrine\ORM\QueryBuilder |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $queryBuilder; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Doctrine\ORM\Mapping\ClassMetadata |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $classMetadata; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $entitys = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $queryFields = []; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct($repository) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->repository = $repository; |
|
|
|
|
|
|
40
|
|
|
$this->queryBuilder = $this->repository->createQueryBuilder(self::DEFAULT_TABLE_ALIAS); |
|
|
|
|
|
|
41
|
|
|
$this->classMetadata = $this->repository->getClassMetadata(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Retorna o nome da entity. |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
|
|
|
|
|
48
|
|
|
*/ |
|
49
|
|
|
public function getEntityName() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->repository->getEntityName(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Retorna a chave primária da entity. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getPrimaryKeyEntity() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getClassMetaData()->identifier[0]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Doctrine\ORM\Query |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getQuery() |
|
68
|
|
|
{ |
|
69
|
|
|
$query = $this->queryBuilder->getQuery(); |
|
70
|
|
|
|
|
71
|
|
|
if (method_exists($query, 'setHint')) { |
|
72
|
|
|
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, true); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $query; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Retorna um array com os objetos do resultado de $this->queryBuilder. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getResult() |
|
84
|
|
|
{ |
|
85
|
|
|
$query = $this->getQuery(); |
|
86
|
|
|
|
|
87
|
|
|
if (method_exists($query, 'getResult')) { |
|
88
|
|
|
return $query->getResult(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $query->execute(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Retorna um objeto do resultado de $this->queryBuilder. |
|
96
|
|
|
* |
|
97
|
|
|
* @return Bludata\Doctrine\ORM\Entities\BaseEntity | null |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getOneResult() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getQuery()->getOneOrNullResult(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Converte os objetos de $this->getResult() em array. |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function toArray(array $options = null) |
|
110
|
|
|
{ |
|
111
|
|
|
$array = []; |
|
112
|
|
|
|
|
113
|
|
|
foreach ($this->getResult() as $item) { |
|
114
|
|
|
if (method_exists($item, 'toArray')) { |
|
115
|
|
|
array_push($array, $item->toArray($options)); |
|
116
|
|
|
} else { |
|
117
|
|
|
array_push($array, $item); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $array; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return Doctrine\ORM\Mapping\ClassMetadata |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getClassMetaData() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->classMetadata; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Retorna a quantidade de elementos em $this->getResult(). |
|
134
|
|
|
* |
|
135
|
|
|
* @return int |
|
136
|
|
|
*/ |
|
137
|
|
|
public function count() |
|
138
|
|
|
{ |
|
139
|
|
|
return count($this->getResult()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Aplica filtros em $this->queryBuilder. |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $filters |
|
|
|
|
|
|
146
|
|
|
* |
|
147
|
|
|
* @return Bludata\Doctrine\ORM\Repositories\QueryWorker |
|
|
|
|
|
|
148
|
|
|
*/ |
|
149
|
|
|
public function withFilters(array $filters = null) |
|
150
|
|
|
{ |
|
151
|
|
|
if ($filters) { |
|
152
|
|
|
foreach ($filters as $filter) { |
|
153
|
|
|
switch ($filter['type']) { |
|
154
|
|
|
case 'select': |
|
155
|
|
|
$this->select($filter['fields']); |
|
156
|
|
|
break; |
|
157
|
|
|
case 'andWhere': |
|
158
|
|
|
$this->andWhere($filter['field'], $filter['operation'], $filter['value']); |
|
|
|
|
|
|
159
|
|
|
break; |
|
160
|
|
|
case 'orWhere': |
|
161
|
|
|
$this->orWhere($filter['field'], $filter['operation'], $filter['value']); |
|
|
|
|
|
|
162
|
|
|
break; |
|
163
|
|
|
case 'andHaving': |
|
164
|
|
|
$this->andHaving($filter['field'], $filter['operation'], $filter['value']); |
|
|
|
|
|
|
165
|
|
|
break; |
|
166
|
|
|
case 'orHaving': |
|
167
|
|
|
$this->orHaving($filter['field'], $filter['operation'], $filter['value']); |
|
|
|
|
|
|
168
|
|
|
break; |
|
169
|
|
|
case 'addGroupBy': |
|
170
|
|
|
$this->addGroupBy($filter['field']); |
|
171
|
|
|
break; |
|
172
|
|
|
case 'addOrderBy': |
|
173
|
|
|
$this->addOrderBy($filter['field'], $filter['order']); |
|
174
|
|
|
break; |
|
175
|
|
|
case 'fkAddOrderBy': |
|
176
|
|
|
$this->fkAddOrderBy($filter['field'], $filter['fkField'], $filter['order']); |
|
|
|
|
|
|
177
|
|
|
break; |
|
178
|
|
|
case 'paginate': |
|
179
|
|
|
if (isset($filter['page'])) { |
|
180
|
|
|
$this->paginate($filter['limit'], $filter['page']); |
|
181
|
|
|
} else { |
|
182
|
|
|
$this->paginate($filter['limit']); |
|
183
|
|
|
} |
|
184
|
|
|
break; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Set the page with paginate attribute. |
|
194
|
|
|
* |
|
195
|
|
|
* @param int $page |
|
196
|
|
|
* @param int $limit |
|
197
|
|
|
* |
|
198
|
|
|
* @return $this |
|
199
|
|
|
*/ |
|
200
|
|
|
public function paginate($limit = 25, $page = 0) |
|
201
|
|
|
{ |
|
202
|
|
|
if ($limit > 0) { |
|
203
|
|
|
$this->queryBuilder->setMaxResults($limit); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
if ($page > 0) { |
|
207
|
|
|
$this->queryBuilder->setFirstResult($page * $limit); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Add joins. |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $field |
|
217
|
|
|
* |
|
218
|
|
|
* @return $array |
|
|
|
|
|
|
219
|
|
|
*/ |
|
220
|
|
|
private function whereFieldJoin($field, $value = null, $operation = null) |
|
221
|
|
|
{ |
|
222
|
|
|
$arr = explode('.', $field); |
|
|
|
|
|
|
223
|
|
|
$tempField = end($arr); |
|
224
|
|
|
$alias = prev($arr); |
|
|
|
|
|
|
225
|
|
|
// verifica se está solicitando um many to many |
|
226
|
|
|
$meta = $this->getClassMetaData(); |
|
227
|
|
|
if (!empty($meta->associationMappings[$alias]['joinTable'])) { |
|
228
|
|
|
$table = $this->getFullFieldName($meta->associationMappings[$alias]['fieldName'], self::DEFAULT_TABLE_ALIAS); |
|
|
|
|
|
|
229
|
|
|
$alias = $this->tableAlias(); |
|
230
|
|
|
if (!$operation) { |
|
231
|
|
|
$operation = '='; |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
$condicao = $this->makeExpression($tempField, $operation, $value, $alias); |
|
|
|
|
|
|
234
|
|
|
$this->queryBuilder->leftJoin($table, $alias, 'WITH', $condicao); |
|
235
|
|
|
|
|
236
|
|
|
return ['alias' => $alias, 'field' => $tempField]; |
|
237
|
|
|
} |
|
238
|
|
|
//monta os joins |
|
239
|
|
|
$this->associationQueryFields($field); |
|
240
|
|
|
//monta os dados do where |
|
241
|
|
|
$field = $tempField; |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
return ['alias' => $alias, 'field' => $field]; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Add a "and where" filter. |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $field |
|
250
|
|
|
* @param string $operation |
|
251
|
|
|
* @param string $value |
|
|
|
|
|
|
252
|
|
|
* |
|
253
|
|
|
* @return $this |
|
254
|
|
|
*/ |
|
255
|
|
View Code Duplication |
public function andWhere($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS) |
|
|
|
|
|
|
256
|
|
|
{ |
|
257
|
|
|
if (strpos($field, '.') > 0) { |
|
258
|
|
|
$newValues = $this->whereFieldJoin($field, $value, $operation); |
|
259
|
|
|
$alias = $newValues['alias']; |
|
|
|
|
|
|
260
|
|
|
$field = $newValues['field']; |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
$this->queryBuilder->andWhere($this->makeExpression($field, $operation, $value, $alias)); |
|
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Add a "or where" filter. |
|
269
|
|
|
* |
|
270
|
|
|
* @param string $field |
|
271
|
|
|
* @param string $operation |
|
272
|
|
|
* @param string $value |
|
|
|
|
|
|
273
|
|
|
* |
|
274
|
|
|
* @return $this |
|
275
|
|
|
*/ |
|
276
|
|
View Code Duplication |
public function orWhere($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS) |
|
|
|
|
|
|
277
|
|
|
{ |
|
278
|
|
|
if (strpos($field, '.') > 0) { |
|
279
|
|
|
$newValues = $this->whereFieldJoin($field, $value, $operation); |
|
280
|
|
|
$alias = $newValues['alias']; |
|
|
|
|
|
|
281
|
|
|
$field = $newValues['field']; |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
$this->queryBuilder->orWhere($this->makeExpression($field, $operation, $value, $alias)); |
|
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
return $this; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Add a join filter. |
|
290
|
|
|
* |
|
291
|
|
|
* @param array $meta |
|
292
|
|
|
* @param string $alias |
|
293
|
|
|
* |
|
294
|
|
|
* @return $this |
|
295
|
|
|
*/ |
|
296
|
|
|
public function manyToManyJoin($meta, $alias, $defaultAlias = self::DEFAULT_TABLE_ALIAS) |
|
|
|
|
|
|
297
|
|
|
{ |
|
298
|
|
|
$table = $this->getFullFieldName($meta->associationMappings[$alias]['fieldName'], $defaultAlias); |
|
|
|
|
|
|
299
|
|
|
|
|
300
|
|
|
if (!in_array($table, $this->entitys)) { |
|
301
|
|
|
$this->queryBuilder->join($table, $alias); |
|
302
|
|
|
$this->entitys[] = $table; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
return $this; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Create an array of expressions. |
|
310
|
|
|
* |
|
311
|
|
|
* @param array $conditions |
|
312
|
|
|
* |
|
313
|
|
|
* @return $this |
|
|
|
|
|
|
314
|
|
|
*/ |
|
315
|
|
|
private function makeExpressions($conditions, $alias = self::DEFAULT_TABLE_ALIAS) |
|
|
|
|
|
|
316
|
|
|
{ |
|
317
|
|
|
$expressions = []; |
|
318
|
|
|
foreach ($conditions as $attr) { |
|
319
|
|
|
$field = $attr['field']; |
|
320
|
|
|
if (strpos($field, '.') > 0) { |
|
321
|
|
|
$newValues = $this->whereFieldJoin($field, $attr['value'], $attr['operation']); |
|
|
|
|
|
|
322
|
|
|
$alias = $newValues['alias']; |
|
|
|
|
|
|
323
|
|
|
$field = $newValues['field']; |
|
|
|
|
|
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
$expressions[] = $this->makeExpression($field, $attr['operation'], $attr['value'], $alias); |
|
|
|
|
|
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return $expressions; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Add a "group by" key. |
|
334
|
|
|
* |
|
335
|
|
|
* @param string $field |
|
336
|
|
|
*/ |
|
337
|
|
|
public function addGroupBy($field) |
|
338
|
|
|
{ |
|
339
|
|
|
$alias = self::DEFAULT_TABLE_ALIAS; |
|
|
|
|
|
|
340
|
|
|
$newAliasField = $this->fieldJoin($this->getClassMetaData(), $field, $alias); |
|
|
|
|
|
|
341
|
|
|
$alias = $newAliasField['alias']; |
|
|
|
|
|
|
342
|
|
|
$field = $newAliasField['field']; |
|
|
|
|
|
|
343
|
|
|
if (count($this->queryFields) > 0) { |
|
344
|
|
|
if (!in_array($this->getFullFieldName($field, $alias), $this->queryFields)) { |
|
|
|
|
|
|
345
|
|
|
$this->queryFields[] = $this->getFullFieldName($field, $alias); |
|
346
|
|
|
} |
|
347
|
|
|
foreach ($this->queryFields as $item) { |
|
348
|
|
|
if (strpos($item, '(') === false) { |
|
349
|
|
|
$this->queryBuilder->addGroupBy($item); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
return $this; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Add a "and having" filter. |
|
359
|
|
|
* |
|
360
|
|
|
* @param string $field |
|
361
|
|
|
* @param string $operation |
|
362
|
|
|
* @param string $value |
|
|
|
|
|
|
363
|
|
|
* |
|
364
|
|
|
* @return $this |
|
365
|
|
|
*/ |
|
366
|
|
|
public function andHaving($field, $operation, $value = null) |
|
|
|
|
|
|
367
|
|
|
{ |
|
368
|
|
|
throw new \Exception('Not implemented'); |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* Add a "or having" filter. |
|
373
|
|
|
* |
|
374
|
|
|
* @param string $field |
|
375
|
|
|
* @param string $order |
|
|
|
|
|
|
376
|
|
|
*/ |
|
377
|
|
|
public function orHaving($field, $operation, $value = null) |
|
|
|
|
|
|
378
|
|
|
{ |
|
379
|
|
|
throw new \Exception('Not implemented'); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Add a "order by" filter. |
|
384
|
|
|
* |
|
385
|
|
|
* @param string $field |
|
386
|
|
|
* @param string $order |
|
387
|
|
|
*/ |
|
388
|
|
View Code Duplication |
public function addOrderBy($field, $order = 'ASC') |
|
|
|
|
|
|
389
|
|
|
{ |
|
390
|
|
|
$alias = self::DEFAULT_TABLE_ALIAS; |
|
391
|
|
|
if (strpos($field, '.') > 0) { |
|
392
|
|
|
$newValues = $this->whereFieldJoin($field); |
|
393
|
|
|
$alias = $newValues['alias']; |
|
|
|
|
|
|
394
|
|
|
$field = $newValues['field']; |
|
|
|
|
|
|
395
|
|
|
} |
|
396
|
|
|
$this->queryBuilder->addOrderBy($this->getFullFieldName($field, $alias), $order); |
|
|
|
|
|
|
397
|
|
|
|
|
398
|
|
|
return $this; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Add a "order by" filter. |
|
403
|
|
|
* |
|
404
|
|
|
* @param string $field |
|
405
|
|
|
* @param string $order |
|
406
|
|
|
*/ |
|
407
|
|
|
public function fkAddOrderBy($field, $fkField, $order = 'ASC') |
|
408
|
|
|
{ |
|
409
|
|
|
$alias = $this->tableAlias(); |
|
410
|
|
|
$this->queryBuilder->join($this->getFullFieldName($field), $alias); |
|
411
|
|
|
$this->queryBuilder->addOrderBy($this->getFullFieldName($fkField, $alias), $order); |
|
|
|
|
|
|
412
|
|
|
|
|
413
|
|
|
return $this; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* Add a select statement. |
|
418
|
|
|
* |
|
419
|
|
|
* @param associationField.fkField |
|
420
|
|
|
* @param $field |
|
421
|
|
|
*/ |
|
422
|
|
|
public function select($fields) |
|
423
|
|
|
{ |
|
424
|
|
|
foreach ($fields as $key => $value) { |
|
425
|
|
|
if (is_int($key)) { |
|
426
|
|
|
$valor = $value; |
|
427
|
|
|
if (is_array($value)) { |
|
428
|
|
|
if (!empty($value['expression'])) { |
|
429
|
|
|
//é uma expressão |
|
430
|
|
|
$expression = ['expression' => $value['expression'], 'alias' => $value['alias']]; |
|
|
|
|
|
|
431
|
|
|
$valor = $value['field']; |
|
|
|
|
|
|
432
|
|
|
$this->associationQueryFields($valor, $expression); |
|
|
|
|
|
|
433
|
|
|
} |
|
434
|
|
|
} else { |
|
435
|
|
|
$this->associationQueryFields($valor); |
|
436
|
|
|
} |
|
437
|
|
|
} elseif (is_array($value)) { |
|
438
|
|
|
$alias = $this->tableAlias(); |
|
439
|
|
|
$this->queryBuilder->join($this->getFullFieldName($key, self::DEFAULT_TABLE_ALIAS), $alias); |
|
|
|
|
|
|
440
|
|
|
foreach ($value as $valueField) { |
|
441
|
|
|
$this->queryFields[] = $this->getFullFieldName($valueField, $alias); |
|
|
|
|
|
|
442
|
|
|
} |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
$this->queryBuilder->select(implode(',', $this->queryFields)); |
|
446
|
|
|
|
|
447
|
|
|
return $this; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
/** |
|
451
|
|
|
* get the repository. |
|
452
|
|
|
* |
|
453
|
|
|
* @param associationField.fkField |
|
454
|
|
|
*/ |
|
455
|
|
|
private function getPathRepository($newEntity) |
|
|
|
|
|
|
456
|
|
|
{ |
|
457
|
|
|
return app()->getRepositoryInterface($newEntity); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* get the class metadata. |
|
462
|
|
|
* |
|
463
|
|
|
* @param associationField.fkField |
|
464
|
|
|
*/ |
|
465
|
|
|
private function getMetaRepository($entity) |
|
|
|
|
|
|
466
|
|
|
{ |
|
467
|
|
|
$repository = $this->getPathRepository($entity); |
|
468
|
|
|
|
|
469
|
|
|
return $repository ? $repository->getClassMetaData() : []; |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
/** |
|
473
|
|
|
* Add association join and select fields. |
|
474
|
|
|
* |
|
475
|
|
|
* @param associationField.fkField |
|
476
|
|
|
* @param $field |
|
477
|
|
|
*/ |
|
478
|
|
|
public function associationQueryFields($value, $expression = 0) |
|
479
|
|
|
{ |
|
480
|
|
|
$pos = strpos($value, '.'); |
|
481
|
|
|
if ($pos > 0) { |
|
482
|
|
|
$fk = substr($value, 0, $pos); |
|
|
|
|
|
|
483
|
|
|
$field = substr($value, ($pos + 1)); |
|
484
|
|
|
$alias = $fk; |
|
485
|
|
|
if (substr_count($value, '.') > 1) { |
|
486
|
|
|
// tem mais de um join para chegar ao campo |
|
487
|
|
|
$count = 0; |
|
|
|
|
|
|
488
|
|
|
$arr = explode('.', $value); |
|
|
|
|
|
|
489
|
|
|
$field = end($arr); |
|
|
|
|
|
|
490
|
|
|
$fkTemp = $fk; |
|
|
|
|
|
|
491
|
|
|
$arrLength = count($arr); |
|
492
|
|
|
foreach ($arr as $key => $entity) { |
|
493
|
|
|
if ($count == 0) { |
|
494
|
|
|
$campo = $this->fkAssociation($this->getClassMetaData(), $entity, $arr[$count + 1], $entity, self::DEFAULT_TABLE_ALIAS); |
|
|
|
|
|
|
495
|
|
|
if ($campo && !in_array($campo, $this->queryFields)) { |
|
496
|
|
|
if ($expression == 0) { |
|
497
|
|
|
$this->queryFields[] = $campo; |
|
498
|
|
|
} |
|
499
|
|
|
} |
|
500
|
|
|
} elseif (($count + 1) < $arrLength) { |
|
501
|
|
|
if ($this->getPathRepository(ucfirst($fkTemp))) { |
|
502
|
|
|
$meta = $this->getMetaRepository(ucfirst($fkTemp)); |
|
|
|
|
|
|
503
|
|
|
$campo = $this->fkAssociation($meta, $entity, $arr[$count + 1], $entity, $fkTemp); |
|
|
|
|
|
|
504
|
|
|
} else { |
|
505
|
|
|
//busca a entidade correta |
|
506
|
|
|
if ($count > 1) { |
|
507
|
|
|
$metaAnterior = null; |
|
508
|
|
|
$fkAnterior = ucfirst($arr[$count - 2]); |
|
|
|
|
|
|
509
|
|
|
//verifica se a entidade anterior era um oneToMany |
|
|
|
|
|
|
510
|
|
|
if (count($this->getPathRepository($fkAnterior)) == 0) { |
|
|
|
|
|
|
511
|
|
|
//verifica se havia uma entidade antes |
|
512
|
|
|
if (!empty($arr[$count - 3])) { |
|
513
|
|
|
$fkAnterior = ucfirst($arr[$count - 3]); |
|
514
|
|
|
} else { |
|
515
|
|
|
// pega a entidade default |
|
516
|
|
|
$fkAnterior = self::DEFAULT_TABLE_ALIAS; |
|
|
|
|
|
|
517
|
|
|
$metaAnterior = $meta; |
|
|
|
|
|
|
518
|
|
|
} |
|
519
|
|
|
} |
|
520
|
|
|
$metaAlias = $this->getFkMetaAlias($fkAnterior, $fkTemp, $metaAnterior); |
|
|
|
|
|
|
521
|
|
|
} else { |
|
522
|
|
|
$metaAlias = $this->getFkMetaAlias(self::DEFAULT_TABLE_ALIAS, $fkTemp, $this->getClassMetaData()); |
|
|
|
|
|
|
523
|
|
|
} |
|
524
|
|
|
$fkTemp = $metaAlias['alias']; |
|
525
|
|
|
$meta = $this->getMetaRepository(ucfirst($fkTemp)); |
|
|
|
|
|
|
526
|
|
|
$campo = $this->fkAssociation($meta, $entity, $arr[$count + 1], $entity, $fkTemp); |
|
|
|
|
|
|
527
|
|
|
} |
|
528
|
|
|
if ($campo && ($expression == 0 || ($expression != 0 && $count == ($arrLength - 1)))) { |
|
|
|
|
|
|
529
|
|
|
$this->addQueryField($campo, $expression); |
|
530
|
|
|
} |
|
531
|
|
|
} |
|
532
|
|
|
$fkTemp = $entity; |
|
533
|
|
|
++$count; |
|
534
|
|
|
} |
|
535
|
|
|
} else { |
|
536
|
|
|
if (empty($this->getClassMetaData()->associationMappings[$fk]) && count($this->getClassMetaData()->subClasses) > 0) { |
|
|
|
|
|
|
537
|
|
|
//ignorado não é possível criar o join, provavelmente está no pai chamando o filho. |
|
|
|
|
|
|
538
|
|
|
return $this; |
|
539
|
|
|
} |
|
540
|
|
|
// realiza o join para retornar o valor do campo |
|
541
|
|
|
$campo = $this->fkAssociation($this->getClassMetaData(), $fk, $field, $alias, self::DEFAULT_TABLE_ALIAS); |
|
|
|
|
|
|
542
|
|
|
$this->addQueryField($campo, $expression); |
|
543
|
|
|
} |
|
544
|
|
|
} else { |
|
545
|
|
|
$valor = $this->getFullFieldName($value); |
|
546
|
|
|
if (!empty($this->getClassMetaData()->associationMappings[$value])) { |
|
|
|
|
|
|
547
|
|
|
// é uma FK, retorna o ID |
|
548
|
|
|
$valor = 'IDENTITY('.$valor.') '.$value; |
|
549
|
|
|
} |
|
550
|
|
|
$this->addQueryField($valor, $expression); |
|
551
|
|
|
} |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
/** |
|
555
|
|
|
* Add a field or expression in the select array. |
|
556
|
|
|
* |
|
557
|
|
|
* @param string field |
|
558
|
|
|
* @param mix expression |
|
559
|
|
|
*/ |
|
560
|
|
|
private function addQueryField($campo, $expression = 0) |
|
561
|
|
|
{ |
|
562
|
|
|
if ($campo && !in_array($campo, $this->queryFields)) { |
|
563
|
|
|
if ($expression == 0) { |
|
564
|
|
|
$this->queryFields[] = $campo; |
|
565
|
|
|
} else { |
|
566
|
|
|
//verifica se pode adicionar a expressão |
|
567
|
|
|
if (strpos($campo, ')') === false) { |
|
568
|
|
|
$this->getSelectExpression($expression['expression'], $campo, $expression['alias']); |
|
|
|
|
|
|
569
|
|
|
} else { |
|
570
|
|
|
$parts = explode(')', $campo); |
|
571
|
|
|
//remove o alias |
|
572
|
|
|
array_pop($parts); |
|
573
|
|
|
$this->getSelectExpression($expression['expression'], implode(')', $parts), $expression['alias']); |
|
|
|
|
|
|
574
|
|
|
} |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Add a join statement. |
|
581
|
|
|
* |
|
582
|
|
|
* @todo ignorar registros com deletedAt == true |
|
|
|
|
|
|
583
|
|
|
* @todo validar a associação a partir do aluno: processos.ordensServico.servicoOrdemServico.itensServicoOrdemServico.itemServico |
|
|
|
|
|
|
584
|
|
|
* |
|
585
|
|
|
* @param $meta - getClassMetaData |
|
586
|
|
|
* @param $fk |
|
587
|
|
|
* @param $field |
|
588
|
|
|
* @param $alias |
|
589
|
|
|
* @param $defaultAlias |
|
590
|
|
|
*/ |
|
591
|
|
|
public function fkAssociation($meta, $fk, $field, $alias, $defaultAlias) |
|
592
|
|
|
{ |
|
593
|
|
|
if ($association = $meta->associationMappings[$fk]) { |
|
594
|
|
|
if (!in_array($association['targetEntity'], $this->entitys)) { |
|
595
|
|
|
if (!empty($association['joinColumns'])) { |
|
596
|
|
|
$condition = $this->getFullFieldName($association['fieldName'], $defaultAlias).' = '.$this->getFullFieldName($association['joinColumns'][0]['referencedColumnName'], $alias); |
|
|
|
|
|
|
597
|
|
|
$this->queryBuilder->leftJoin($association['targetEntity'], $alias, 'WITH', $condition); |
|
|
|
|
|
|
598
|
|
|
$this->queryBuilder->andWhere($condition); |
|
599
|
|
|
$this->entitys[] = $association['targetEntity']; |
|
600
|
|
|
} else { |
|
601
|
|
|
//está buscando de um arrayCollection |
|
602
|
|
|
$repository = explode('\\', $association['targetEntity']); |
|
603
|
|
|
if (!$association['mappedBy']) { |
|
604
|
|
|
if (!empty($association['joinTable'])) { |
|
605
|
|
|
$this->manyToManyJoin($meta, $fk, $defaultAlias); |
|
606
|
|
|
|
|
607
|
|
|
return $this->getFullFieldName($field, $fk); |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
return; |
|
611
|
|
|
} |
|
612
|
|
|
$meta = $this->getMetaRepository(end($repository)); |
|
|
|
|
|
|
613
|
|
|
|
|
614
|
|
|
return $this->fkArrayAssociation($meta, $association['mappedBy'], $field, lcfirst(end($repository)), $defaultAlias, $association['targetEntity']); |
|
|
|
|
|
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
//verifica se o campo existe |
|
619
|
|
|
if ($this->getPathRepository(ucfirst($fk))) { |
|
620
|
|
|
$meta = $this->getMetaRepository(ucfirst($fk)); |
|
|
|
|
|
|
621
|
|
|
} else { |
|
622
|
|
|
//busca a entidade correta |
|
623
|
|
|
$metaAlias = $this->getFkMetaAlias($defaultAlias, $fk, $meta); |
|
624
|
|
|
$meta = $metaAlias['meta']; |
|
|
|
|
|
|
625
|
|
|
$alias = $metaAlias['alias']; |
|
|
|
|
|
|
626
|
|
|
} |
|
627
|
|
|
if (empty($meta->associationMappings[$field]) && empty($meta->fieldMappings[$field])) { |
|
|
|
|
|
|
628
|
|
|
return; |
|
629
|
|
|
} elseif (!empty($meta->associationMappings[$field]) && empty($meta->associationMappings[$field]['joinColumns'])) { |
|
|
|
|
|
|
630
|
|
|
return; |
|
631
|
|
|
} |
|
632
|
|
|
//retorna o campo |
|
633
|
|
|
return '('.$this->getFullFieldName($field, $alias).') AS '.$this->getFullFieldName($field, $alias, '_'); |
|
|
|
|
|
|
634
|
|
|
} |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
|
|
/** |
|
638
|
|
|
* Add joins and return the new field and alias. |
|
639
|
|
|
* |
|
640
|
|
|
* @param $meta - getClassMetaData |
|
641
|
|
|
* @param $field |
|
642
|
|
|
* @param $alias |
|
643
|
|
|
* |
|
644
|
|
|
* @return array |
|
645
|
|
|
*/ |
|
646
|
|
|
public function fieldJoin($meta, $field, $alias) |
|
647
|
|
|
{ |
|
648
|
|
|
if (strpos($field, '.') > 0) { |
|
649
|
|
|
$arr = explode('.', $field); |
|
|
|
|
|
|
650
|
|
|
$fieldTemp = end($arr); |
|
651
|
|
|
$alias = prev($arr); |
|
|
|
|
|
|
652
|
|
|
if (!empty($meta->associationMappings[$alias]['joinTable'])) { |
|
653
|
|
|
$this->manyToManyJoin($meta, $alias); |
|
654
|
|
|
} else { |
|
655
|
|
|
//monta os joins |
|
656
|
|
|
$this->associationQueryFields($field); |
|
657
|
|
|
} |
|
658
|
|
|
$field = $fieldTemp; |
|
|
|
|
|
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
return ['alias' => $alias, 'field' => $field]; |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
|
|
/** |
|
665
|
|
|
* @TODO GROUP_CONCAT() |
|
|
|
|
|
|
666
|
|
|
* Add a join statement from array collection |
|
667
|
|
|
* |
|
668
|
|
|
* @param $meta - getClassMetaData |
|
669
|
|
|
* @param $fk |
|
670
|
|
|
* @param $field |
|
671
|
|
|
* @param $alias |
|
672
|
|
|
* @param $defaultAlias |
|
673
|
|
|
* @param $targetEntity |
|
674
|
|
|
*/ |
|
675
|
|
|
public function fkArrayAssociation($meta, $fk, $field, $alias, $defaultAlias, $targetEntity) |
|
|
|
|
|
|
676
|
|
|
{ |
|
677
|
|
|
if ($association = $meta->associationMappings[$fk]) { |
|
678
|
|
|
if (!in_array($targetEntity, $this->entitys)) { |
|
679
|
|
|
$condition = $this->getFullFieldName($association['fieldName'], $alias).' = '. |
|
|
|
|
|
|
680
|
|
|
$this->getFullFieldName($association['joinColumns'][0]['referencedColumnName'], $defaultAlias); |
|
|
|
|
|
|
681
|
|
|
$this->queryBuilder->join($targetEntity, $alias, 'WITH', $condition); |
|
|
|
|
|
|
682
|
|
|
$this->queryBuilder->andWhere($condition); |
|
683
|
|
|
$this->entitys[] = $targetEntity; |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
return '('.$this->getFullFieldName($field, $alias).') AS '.$this->getFullFieldName($field, $alias, '_'); |
|
|
|
|
|
|
687
|
|
|
} |
|
688
|
|
|
} |
|
689
|
|
|
|
|
690
|
|
|
/** |
|
691
|
|
|
* @param string $alias |
|
692
|
|
|
* @param string $fk |
|
693
|
|
|
* @param array $meta |
|
|
|
|
|
|
694
|
|
|
* |
|
695
|
|
|
* @return array |
|
696
|
|
|
*/ |
|
697
|
|
|
public function getFkMetaAlias($alias, $fk, $meta = null) |
|
698
|
|
|
{ |
|
699
|
|
|
if (self::DEFAULT_TABLE_ALIAS != $alias) { |
|
700
|
|
|
$meta = $this->getMetaRepository(ucfirst($alias)); |
|
|
|
|
|
|
701
|
|
|
} |
|
702
|
|
|
$repository = explode('\\', $meta->associationMappings[$fk]['targetEntity']); |
|
|
|
|
|
|
703
|
|
|
$meta = $this->getMetaRepository(end($repository)); |
|
|
|
|
|
|
704
|
|
|
$alias = lcfirst(end($repository)); |
|
|
|
|
|
|
705
|
|
|
|
|
706
|
|
|
return ['meta' => $meta, 'alias' => $alias]; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* @param mixed $field |
|
711
|
|
|
* @param string $expression |
|
712
|
|
|
* @param string $alias |
|
713
|
|
|
*/ |
|
714
|
|
|
private function getSelectExpression($expression, $field, $alias, $fieldAlias = self::DEFAULT_TABLE_ALIAS) |
|
|
|
|
|
|
715
|
|
|
{ |
|
716
|
|
|
$validExpressions = ['SUM', 'MIN', 'MAX', 'AVG', 'COUNT']; |
|
717
|
|
|
if (in_array(trim(strtoupper($expression)), $validExpressions)) { |
|
718
|
|
|
if (strpos($field, '.') === false) { |
|
719
|
|
|
$field = getFullFieldName($field, $fieldAlias); |
|
|
|
|
|
|
720
|
|
|
} |
|
721
|
|
|
$this->queryFields[] = sprintf('%s(%s) AS %s', $expression, $field, $alias); |
|
|
|
|
|
|
722
|
|
|
} |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
|
|
/** |
|
726
|
|
|
* @param $field |
|
727
|
|
|
* @param string $alias |
|
728
|
|
|
* |
|
729
|
|
|
* @return string |
|
730
|
|
|
*/ |
|
731
|
|
|
protected function getFullFieldName($field, $alias = self::DEFAULT_TABLE_ALIAS, $separator = '.') |
|
|
|
|
|
|
732
|
|
|
{ |
|
733
|
|
|
return sprintf('%s%s%s', $alias, $separator, $field); |
|
734
|
|
|
} |
|
735
|
|
|
|
|
736
|
|
|
/** |
|
737
|
|
|
* @param $field |
|
738
|
|
|
* @param $operation |
|
739
|
|
|
* @param null $value |
|
740
|
|
|
* @param string $alias |
|
741
|
|
|
*/ |
|
742
|
|
|
protected function makeExpression($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS) |
|
|
|
|
|
|
743
|
|
|
{ |
|
744
|
|
|
if (!is_array($value)) { |
|
745
|
|
|
$value = $this->queryBuilder->expr()->literal($value); |
|
|
|
|
|
|
746
|
|
|
} |
|
747
|
|
|
if ($field) { |
|
748
|
|
|
$field = $this->getFullFieldName($field, $alias); |
|
|
|
|
|
|
749
|
|
|
} |
|
750
|
|
|
$expression = null; |
|
751
|
|
|
switch (strtolower($operation)) { |
|
752
|
|
|
case '>': |
|
753
|
|
|
$expression = $this->queryBuilder->expr()->gt($field, $value); |
|
754
|
|
|
break; |
|
755
|
|
|
case '=': |
|
756
|
|
|
$expression = $this->queryBuilder->expr()->eq($field, $value); |
|
757
|
|
|
break; |
|
758
|
|
|
case '<': |
|
759
|
|
|
$expression = $this->queryBuilder->expr()->lt($field, $value); |
|
760
|
|
|
break; |
|
761
|
|
|
case '>=': |
|
762
|
|
|
$expression = $this->queryBuilder->expr()->gte($field, $value); |
|
763
|
|
|
break; |
|
764
|
|
|
case '<=': |
|
765
|
|
|
$expression = $this->queryBuilder->expr()->lte($field, $value); |
|
766
|
|
|
break; |
|
767
|
|
|
case '<>': |
|
768
|
|
|
$expression = $this->queryBuilder->expr()->neq($field, $value); |
|
769
|
|
|
break; |
|
770
|
|
|
case 'isnull': |
|
771
|
|
|
$expression = $this->queryBuilder->expr()->isNull($field); |
|
772
|
|
|
break; |
|
773
|
|
|
case 'isnotnull': |
|
774
|
|
|
$expression = $this->queryBuilder->expr()->isNotNull($field); |
|
775
|
|
|
break; |
|
776
|
|
|
case 'in': |
|
777
|
|
|
$expression = $this->queryBuilder->expr()->in($field, $value); |
|
778
|
|
|
break; |
|
779
|
|
|
case 'orx': |
|
780
|
|
|
$expression = $this->queryBuilder->expr()->orX()->addMultiple($this->makeExpressions($value, $alias)); |
|
|
|
|
|
|
781
|
|
|
break; |
|
782
|
|
|
case 'andx': |
|
783
|
|
|
$expression = $this->queryBuilder->expr()->andX()->addMultiple($this->makeExpressions($value, $alias)); |
|
|
|
|
|
|
784
|
|
|
break; |
|
785
|
|
|
case 'notin': |
|
786
|
|
|
$expression = $this->queryBuilder->expr()->notIn($field, $value); |
|
|
|
|
|
|
787
|
|
|
break; |
|
788
|
|
|
case 'contains': |
|
789
|
|
|
/* |
|
790
|
|
|
* @todo implementar o metodo contains |
|
|
|
|
|
|
791
|
|
|
*/ |
|
792
|
|
|
// $expression = $this->queryBuilder->expr()->contains($field, $value); |
|
|
|
|
|
|
793
|
|
|
break; |
|
794
|
|
|
case 'like': |
|
795
|
|
|
$expression = $this->queryBuilder->expr()->like('LOWER('.$field.')', strtolower($value)); |
|
|
|
|
|
|
796
|
|
|
break; |
|
797
|
|
|
case 'notlike': |
|
798
|
|
|
$expression = $this->queryBuilder->expr()->notLike($field, $value); |
|
|
|
|
|
|
799
|
|
|
break; |
|
800
|
|
|
case 'isinstanceof': |
|
801
|
|
|
$expression = $alias.' INSTANCE OF '.$value; |
|
802
|
|
|
break; |
|
803
|
|
|
case 'between': |
|
804
|
|
|
$expression = $this->queryBuilder->expr()->between($field, $this->queryBuilder->expr()->literal($value[0]), $this->queryBuilder->expr()->literal($value[1])); |
|
|
|
|
|
|
805
|
|
|
break; |
|
806
|
|
|
case 'dateparteq': |
|
807
|
|
|
$expression = $this->queryBuilder->expr()->eq("DATEPART('".$value['format']."', ".$field.')', $value['value']); |
|
|
|
|
|
|
808
|
|
|
} |
|
809
|
|
|
|
|
810
|
|
|
return $expression; |
|
811
|
|
|
} |
|
812
|
|
|
|
|
813
|
|
|
/** |
|
814
|
|
|
* @return string |
|
815
|
|
|
*/ |
|
816
|
|
|
protected function tableAlias() |
|
817
|
|
|
{ |
|
818
|
|
|
return self::DEFAULT_TABLE_ALIAS.count($this->queryBuilder->getAllAliases()); |
|
|
|
|
|
|
819
|
|
|
} |
|
820
|
|
|
} |
|
821
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.