1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Queries; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Mado\QueryBundle\Dictionary\Operators; |
7
|
|
|
use Mado\QueryBundle\Services\ConfigProvider; |
8
|
|
|
|
9
|
|
|
class QueryBuilderFactory extends AbstractQuery |
10
|
|
|
{ |
11
|
|
|
const DIRECTION_AZ = 'asc'; |
12
|
|
|
|
13
|
|
|
const DIRECTION_ZA = 'desc'; |
14
|
|
|
|
15
|
|
|
const DEFAULT_OPERATOR = 'eq'; |
16
|
|
|
|
17
|
|
|
protected $qBuilder; |
18
|
|
|
|
19
|
|
|
protected $fields; |
20
|
|
|
|
21
|
|
|
protected $filtering; |
22
|
|
|
|
23
|
|
|
protected $orFiltering; |
24
|
|
|
|
25
|
|
|
protected $relationEntityAlias; |
26
|
|
|
|
27
|
|
|
protected $sorting; |
28
|
|
|
|
29
|
|
|
protected $joins; |
30
|
|
|
|
31
|
|
|
protected $entityAliases; |
32
|
|
|
|
33
|
|
|
protected $rel; |
34
|
|
|
|
35
|
|
|
protected $printing; |
36
|
|
|
|
37
|
|
|
protected $page; |
38
|
|
|
|
39
|
|
|
protected $pageLength; |
40
|
|
|
|
41
|
|
|
protected $select; |
42
|
|
|
|
43
|
|
|
protected $configProvider; |
44
|
|
|
|
45
|
|
|
public function getAvailableFilters() |
46
|
|
|
{ |
47
|
|
|
return array_keys($this->getValueAvailableFilters()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getValueAvailableFilters() |
51
|
|
|
{ |
52
|
|
|
return Operators::getOperators(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setFields(array $fields = []) |
56
|
|
|
{ |
57
|
|
|
$this->fields = $fields; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getFields() |
63
|
|
|
{ |
64
|
|
|
if (null === $this->fields) { |
65
|
|
|
throw new \RuntimeException( |
66
|
|
|
'Oops! Fields are not defined' |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->fields; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setFilters(array $filtering = []) |
74
|
|
|
{ |
75
|
|
|
$this->filtering = $filtering; |
76
|
1 |
|
|
77
|
|
|
return $this; |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
public function setOrFilters(array $orFiltering = []) |
81
|
|
|
{ |
82
|
|
|
$this->orFiltering = $orFiltering; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setSorting(array $sorting = []) |
88
|
|
|
{ |
89
|
|
|
$this->sorting = $sorting; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getFilters() |
95
|
|
|
{ |
96
|
|
|
return $this->filtering; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getOrFilters() |
100
|
|
|
{ |
101
|
|
|
return $this->orFiltering; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function noExistsJoin($prevEntityAlias, $relationEntityAlias) |
105
|
|
|
{ |
106
|
|
|
if (null === $this->joins) { |
107
|
|
|
$this->joins = []; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$needle = $prevEntityAlias . "_" . $relationEntityAlias; |
111
|
|
|
|
112
|
|
|
//$this->entityAliases[$relationEntityAlias]['alias'] = $prevEntityAlias . "_" . $relationEntityAlias; |
113
|
|
|
|
114
|
|
|
return ! in_array($needle, $this->joins); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function storeJoin($relationEntityAlias, $relation, $association) |
118
|
|
|
{ |
119
|
|
|
$needle = $relationEntityAlias . "_" . $relation; |
120
|
|
|
$this->joins[$needle] = $needle; |
121
|
|
|
$this->entityAliases[$relation] = $association; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name) |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function join(String $relation) |
129
|
|
|
{ |
130
|
|
|
$relation = explode('|', $relation)[0]; |
131
|
|
|
$relations = [$relation]; |
132
|
|
|
|
133
|
|
|
if (strstr($relation, '_embedded.')) { |
134
|
|
|
$embeddedFields = explode('.', $relation); |
135
|
|
|
$relation = $this->parser->camelize($embeddedFields[1]); |
|
|
|
|
136
|
|
|
|
137
|
|
|
// elimino l'ultimo elemento che dovrebbe essere il nome del campo |
138
|
|
|
unset($embeddedFields[count($embeddedFields) - 1]); |
139
|
|
|
|
140
|
|
|
// elimino il primo elemento _embedded |
141
|
|
|
unset($embeddedFields[0]); |
142
|
|
|
|
143
|
|
|
$relations = $embeddedFields; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$entityName = $this->getEntityName(); |
147
|
|
|
$entityAlias = $this->entityAlias; |
148
|
|
|
|
149
|
|
|
foreach ($relations as $relation) { |
150
|
|
|
|
151
|
|
|
$relation = $this->parser->camelize($relation); |
152
|
|
|
$relationEntityAlias = 'table_' . $relation; |
153
|
|
|
|
154
|
|
|
$metadata = $this->manager->getClassMetadata($entityName); |
155
|
|
|
|
156
|
|
|
if ($metadata->hasAssociation($relation)) { |
157
|
|
|
|
158
|
|
|
$association = $metadata->getAssociationMapping($relation); |
159
|
|
|
|
160
|
|
|
$fieldName = $this->parser->camelize($association['fieldName']); |
161
|
|
|
|
162
|
|
|
if ($this->noExistsJoin($relationEntityAlias, $relation)) { |
163
|
|
|
$this->qBuilder->join($entityAlias . "." . $fieldName, $relationEntityAlias); |
164
|
|
|
//var_dump($entityAlias); |
165
|
|
|
//die; |
166
|
|
|
$this->storeJoin( |
167
|
|
|
$relationEntityAlias, |
168
|
|
|
$relation, |
169
|
|
|
$association |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$entityName = $association['targetEntity']; |
174
|
|
|
$entityAlias = $relationEntityAlias; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$this->setRelationEntityAlias($relationEntityAlias); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function filter() |
184
|
|
|
{ |
185
|
|
|
if (null === $this->filtering) { |
186
|
|
|
throw new \RuntimeException( |
187
|
|
|
'Oops! Filtering is not defined' |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if (!$this->fields) { |
192
|
|
|
throw new \RuntimeException( |
193
|
|
|
'Oops! Fields are not defined' |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
foreach ($this->filtering as $filter => $value) { |
198
|
|
|
$this->applyFilterAnd($filter, $value); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (null !== $this->orFiltering) { |
202
|
|
|
$orFilter = []; |
203
|
|
|
$orFilter['orCondition'] = null; |
204
|
|
|
$orFilter['parameters'] = []; |
205
|
|
|
|
206
|
|
|
foreach ($this->orFiltering as $filter => $value) { |
207
|
|
|
$orFilter = $this->applyFilterOr($filter, $value, $orFilter); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ((count($orFilter) > 0) && ($orFilter['orCondition'] != null)) { |
211
|
|
|
$this->qBuilder->andWhere($orFilter['orCondition']); |
212
|
|
|
|
213
|
|
|
foreach ($orFilter['parameters'] as $parameter) { |
214
|
|
|
$this->qBuilder->setParameter($parameter['field'], $parameter['value']); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
private function applyFilterAnd($filter, $value) |
223
|
|
|
{ |
224
|
|
|
$whereCondition = null; |
225
|
|
|
$filterAndOperator = explode('|',$filter); |
226
|
|
|
|
227
|
|
|
$fieldName = $filterAndOperator[0]; |
228
|
|
|
$fieldName = $this->parser->camelize($fieldName); |
229
|
|
|
|
230
|
|
|
$operator = $this->getValueAvailableFilters()[self::DEFAULT_OPERATOR]; |
231
|
|
|
if(isset($filterAndOperator[1])){ |
232
|
|
|
$operator = $this->getValueAvailableFilters()[$filterAndOperator[1]]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// controllo se il filtro che mi arriva dalla richiesta è una proprietà di questa entità |
236
|
|
|
// esempio per users: filtering[username|contains]=mado |
237
|
|
|
if (in_array($fieldName, $this->fields)) { |
238
|
|
|
|
239
|
|
|
$salt = ''; |
240
|
|
View Code Duplication |
foreach ($this->qBuilder->getParameters() as $parameter) { |
|
|
|
|
241
|
|
|
if ($parameter->getName() == 'field_' . $fieldName) { |
242
|
|
|
$salt = '_' . rand(111, 999); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// filtering[foo|bar] |
247
|
|
|
// $filterAndOperator[0] = 'foo' |
248
|
|
|
// $filterAndOperator[1] = 'bar' |
249
|
|
View Code Duplication |
if (isset($filterAndOperator[1])) { |
|
|
|
|
250
|
|
|
if ('list' == $filterAndOperator[1]) { |
251
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
252
|
|
|
} else if ('field_eq' == $filterAndOperator[1]) { |
253
|
|
|
$whereCondition = |
254
|
|
|
$this->entityAlias . '.' . $fieldName . ' '. |
255
|
|
|
$operator['meta'] . '' . |
256
|
|
|
$this->entityAlias . '.' . $value |
257
|
|
|
; |
258
|
|
|
//} else { |
259
|
|
|
//throw new \RuntimeException( |
260
|
|
|
//'Oops! Eccezzione' |
261
|
|
|
//); |
262
|
|
|
} else { |
263
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
264
|
|
|
} |
265
|
|
|
} else { |
266
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' = :field_'.$fieldName . $salt; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$this->qBuilder->andWhere($whereCondition); |
270
|
|
|
|
271
|
|
View Code Duplication |
if (isset($operator['substitution_pattern'])) { |
|
|
|
|
272
|
|
|
if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
273
|
|
|
$value = explode(',', $value); |
274
|
|
|
} else { |
275
|
|
|
$value = str_replace( |
276
|
|
|
'{string}', |
277
|
|
|
$value, |
278
|
|
|
$operator['substitution_pattern'] |
279
|
|
|
); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$this->qBuilder->setParameter('field_' . $fieldName . $salt, $value); |
284
|
|
|
} else { |
285
|
|
|
$isNotARelation = 0 !== strpos($fieldName, 'Embedded.'); |
286
|
|
|
if ($isNotARelation) { |
287
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' ' . $this->entityAlias . '.' . $value; |
288
|
|
|
$this->qBuilder->andWhere($whereCondition); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
293
|
|
|
// esempio per users: filtering[_embedded.groups.name|eq]=admin |
294
|
|
|
if (strstr($filter, '_embedded.')) { |
295
|
|
|
|
296
|
|
|
$this->join($filter); |
297
|
|
|
$relationEntityAlias = $this->getRelationEntityAlias(); |
298
|
|
|
|
299
|
|
|
$embeddedFields = explode('.', $fieldName); |
300
|
|
|
$fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields)-1]); |
301
|
|
|
|
302
|
|
|
$salt = ''; |
303
|
|
View Code Duplication |
foreach ($this->qBuilder->getParameters() as $parameter) { |
|
|
|
|
304
|
|
|
if ($parameter->getName() == 'field_' . $fieldName) { |
305
|
|
|
$salt = '_' . rand(111, 999); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
View Code Duplication |
if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
|
|
|
|
310
|
|
|
$whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
311
|
|
|
} else { |
312
|
|
|
$whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$this->qBuilder->andWhere($whereCondition); |
316
|
|
View Code Duplication |
if (isset($operator['substitution_pattern'])) { |
|
|
|
|
317
|
|
|
if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
318
|
|
|
$value = explode(',', $value); |
|
|
|
|
319
|
|
|
} else { |
320
|
|
|
$value = str_replace( |
321
|
|
|
'{string}', |
322
|
|
|
$value, |
323
|
|
|
$operator['substitution_pattern'] |
324
|
|
|
); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$this->qBuilder->setParameter('field_' . $fieldName . $salt, $value); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
private function applyFilterOr($filter, $value, $orCondition) |
333
|
|
|
{ |
334
|
|
|
$whereCondition = null; |
335
|
|
|
$filterAndOperator = explode('|',$filter); |
336
|
|
|
|
337
|
|
|
$fieldName = $filterAndOperator[0]; |
338
|
|
|
$fieldName = $this->parser->camelize($fieldName); |
339
|
|
|
|
340
|
|
|
$operator = $this->getValueAvailableFilters()[self::DEFAULT_OPERATOR]; |
341
|
|
|
if(isset($filterAndOperator[1])){ |
342
|
|
|
$operator = $this->getValueAvailableFilters()[$filterAndOperator[1]]; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
// controllo se il filtro che mi arriva dalla richiesta è una proprietà di questa entità |
346
|
|
|
// esempio per users: filtering[username|contains]=mado |
347
|
|
|
if (in_array($fieldName, $this->fields)) { |
348
|
|
|
|
349
|
|
|
$salt = ''; |
350
|
|
View Code Duplication |
foreach ($this->qBuilder->getParameters() as $parameter) { |
|
|
|
|
351
|
|
|
if ($parameter->getName() == 'field_' . $fieldName) { |
352
|
|
|
$salt = '_' . rand(111, 999); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if ($salt == '') { |
357
|
|
|
$salt = '_' . rand(111, 999); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
// filtering[foo|bar] |
361
|
|
|
// $filterAndOperator[0] = 'foo' |
362
|
|
|
// $filterAndOperator[1] = 'bar' |
363
|
|
View Code Duplication |
if (isset($filterAndOperator[1])) { |
|
|
|
|
364
|
|
|
if ('list' == $filterAndOperator[1]) { |
365
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
366
|
|
|
} else if ('field_eq' == $filterAndOperator[1]) { |
367
|
|
|
$whereCondition = |
368
|
|
|
$this->entityAlias . '.' . $fieldName . ' '. |
369
|
|
|
$operator['meta'] . '' . |
370
|
|
|
$this->entityAlias . '.' . $value |
371
|
|
|
; |
372
|
|
|
//} else { |
373
|
|
|
//throw new \RuntimeException( |
374
|
|
|
//'Oops! Eccezzione' |
375
|
|
|
//); |
376
|
|
|
} else { |
377
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
378
|
|
|
} |
379
|
|
|
} else { |
380
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' = :field_'.$fieldName . $salt; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
View Code Duplication |
if ($orCondition['orCondition'] != null) { |
|
|
|
|
384
|
|
|
$orCondition['orCondition'] .= ' OR ' . $whereCondition; |
385
|
|
|
} else { |
386
|
|
|
$orCondition['orCondition'] = $whereCondition; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
View Code Duplication |
if (isset($operator['substitution_pattern'])) { |
|
|
|
|
390
|
|
|
if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
391
|
|
|
$value = explode(',', $value); |
392
|
|
|
} else { |
393
|
|
|
$value = str_replace( |
394
|
|
|
'{string}', |
395
|
|
|
$value, |
396
|
|
|
$operator['substitution_pattern'] |
397
|
|
|
); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
$orCondition['parameters'][] = [ |
402
|
|
|
'field' => 'field_' . $fieldName . $salt, |
403
|
|
|
'value' => $value |
404
|
|
|
]; |
405
|
|
|
} else { |
406
|
|
|
$isNotARelation = 0 !== strpos($fieldName, 'Embedded.'); |
407
|
|
|
if ($isNotARelation) { |
408
|
|
|
$whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' ' . $this->entityAlias . '.' . $value; |
409
|
|
View Code Duplication |
if ($orCondition['orCondition'] != null) { |
|
|
|
|
410
|
|
|
$orCondition['orCondition'] .= ' OR ' . $whereCondition; |
411
|
|
|
} else { |
412
|
|
|
$orCondition['orCondition'] = $whereCondition; |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
// controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
418
|
|
|
// esempio per users: filtering[_embedded.groups.name|eq]=admin |
419
|
|
|
if (strstr($filter, '_embedded.')) { |
420
|
|
|
|
421
|
|
|
$this->join($filter); |
422
|
|
|
$relationEntityAlias = $this->getRelationEntityAlias(); |
423
|
|
|
|
424
|
|
|
$embeddedFields = explode('.', $fieldName); |
425
|
|
|
$fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields)-1]); |
426
|
|
|
|
427
|
|
|
$salt = ''; |
428
|
|
View Code Duplication |
foreach ($this->qBuilder->getParameters() as $parameter) { |
|
|
|
|
429
|
|
|
if ($parameter->getName() == 'field_' . $fieldName) { |
430
|
|
|
$salt = '_' . rand(111, 999); |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
if ($salt == '') { |
435
|
|
|
$salt = '_' . rand(111, 999); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
View Code Duplication |
if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
|
|
|
|
439
|
|
|
$whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
440
|
|
|
} else { |
441
|
|
|
$whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
View Code Duplication |
if ($orCondition['orCondition'] != null) { |
|
|
|
|
445
|
|
|
$orCondition['orCondition'] .= ' OR ' . $whereCondition; |
446
|
|
|
} else { |
447
|
|
|
$orCondition['orCondition'] = $whereCondition; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
View Code Duplication |
if (isset($operator['substitution_pattern'])) { |
|
|
|
|
451
|
|
|
if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
452
|
|
|
$value = explode(',', $value); |
|
|
|
|
453
|
|
|
} else { |
454
|
|
|
$value = str_replace( |
455
|
|
|
'{string}', |
456
|
|
|
$value, |
457
|
|
|
$operator['substitution_pattern'] |
458
|
|
|
); |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
$orCondition['parameters'][] = [ |
463
|
|
|
'field' => 'field_' . $fieldName . $salt, |
464
|
|
|
'value' => $value |
465
|
|
|
]; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
return $orCondition; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
public function sort() |
472
|
|
|
{ |
473
|
|
|
if (!$this->fields) { |
474
|
|
|
throw new \RuntimeException( |
475
|
|
|
'Oops! Fields are not defined' |
476
|
|
|
); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
if (null === $this->sorting) { |
480
|
|
|
throw new \RuntimeException( |
481
|
|
|
'Oops! Sorting is not defined' |
482
|
|
|
); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
foreach ($this->sorting as $sort => $val) { |
486
|
|
|
$val = strtolower($val); |
487
|
|
|
|
488
|
|
|
$fieldName = $this->parser->camelize($sort); |
489
|
|
|
|
490
|
|
|
if (in_array($fieldName, $this->fields)) { |
491
|
|
|
$direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
492
|
|
|
$this->qBuilder->addOrderBy($this->entityAlias .'.'. $fieldName, $direction); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
if (strstr($sort, '_embedded.')) { |
496
|
|
|
$this->join($sort); |
497
|
|
|
$relationEntityAlias = $this->getRelationEntityAlias(); |
498
|
|
|
|
499
|
|
|
$embeddedFields = explode('.', $sort); |
500
|
|
|
$fieldName = $this->parser->camelize($embeddedFields[2]); |
501
|
|
|
$direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
502
|
|
|
|
503
|
|
|
$this->qBuilder->addOrderBy($relationEntityAlias.'.'.$fieldName, $direction); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
return $this; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
public function getQueryBuilder() |
512
|
|
|
{ |
513
|
|
|
if (!$this->qBuilder) { |
514
|
|
|
throw new \RuntimeException( |
515
|
|
|
"Oops! Query builder was never initialized! call ::createQueryBuilder('entityName', 'alias') to start." |
516
|
|
|
); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
return $this->qBuilder; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
public function buildSelectValue() : string |
523
|
|
|
{ |
524
|
|
|
if ("" == $this->getSelect()) { |
525
|
|
|
return $this->getEntityAlias( |
|
|
|
|
526
|
|
|
$this->getEntityName() |
527
|
|
|
); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
return $this->getSelect(); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
private function setRelationEntityAlias(string $relationEntityAlias) |
534
|
|
|
{ |
535
|
|
|
$this->relationEntityAlias = $relationEntityAlias; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
private function getRelationEntityAlias() |
539
|
|
|
{ |
540
|
|
|
return $this->relationEntityAlias; |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
public function setRel($rel) |
544
|
|
|
{ |
545
|
|
|
$this->rel = $rel; |
546
|
|
|
|
547
|
|
|
return $this; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
public function getRel() |
551
|
|
|
{ |
552
|
|
|
return $this->rel; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
public function setPrinting($printing) |
556
|
|
|
{ |
557
|
|
|
$this->printing = $printing; |
558
|
|
|
|
559
|
|
|
return $this; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
public function getPrinting() |
563
|
|
|
{ |
564
|
|
|
return $this->printing; |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
public function setPage($page) |
568
|
|
|
{ |
569
|
|
|
$this->page = $page; |
570
|
|
|
|
571
|
|
|
return $this; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
public function getPage() |
575
|
|
|
{ |
576
|
|
|
return $this->page; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
public function setPageLength($pageLength) |
580
|
|
|
{ |
581
|
|
|
$this->pageLength = $pageLength; |
582
|
|
|
|
583
|
|
|
return $this; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
public function getPageLength() |
587
|
|
|
{ |
588
|
|
|
return $this->pageLength; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
public function setSelect( $select) : QueryBuilderFactory |
592
|
|
|
{ |
593
|
|
|
$this->select = $select; |
594
|
|
|
|
595
|
|
|
return $this; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
public function getSelect() |
599
|
|
|
{ |
600
|
|
|
return $this->select; |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
public function getEntityManager() |
604
|
|
|
{ |
605
|
|
|
return $this->manager; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
public function setConfigProvider(ConfigProvider $configProvider) |
609
|
|
|
{ |
610
|
|
|
$this->configProvider = $configProvider; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
public function getJoins() |
614
|
|
|
{ |
615
|
|
|
return $this->joins; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
public function getEntityAliases() |
619
|
|
|
{ |
620
|
|
|
return $this->entityAliases; |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|