|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Hateoas\Configuration\Route; |
|
|
|
|
|
|
7
|
|
|
use Hateoas\Representation\Factory\PagerfantaFactory; |
|
|
|
|
|
|
8
|
|
|
use Mado\QueryBundle\Queries\QueryBuilderFactory; |
|
9
|
|
|
use Mado\QueryBundle\Queries\QueryBuilderOptions; |
|
10
|
|
|
use Mado\QueryBundle\Component\ConfigProvider; |
|
11
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
|
|
|
|
|
|
12
|
|
|
use Pagerfanta\Pagerfanta; |
|
|
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
|
|
15
|
|
|
class BaseRepository extends EntityRepository |
|
16
|
|
|
{ |
|
17
|
|
|
protected $fields; |
|
18
|
|
|
|
|
19
|
|
|
protected $request; |
|
20
|
|
|
|
|
21
|
|
|
protected $use_result_cache = false; |
|
22
|
|
|
|
|
23
|
|
|
protected $entityAlias; |
|
24
|
|
|
|
|
25
|
|
|
protected $route_name; |
|
26
|
|
|
|
|
27
|
|
|
protected $currentEntityAlias; |
|
28
|
|
|
|
|
29
|
|
|
protected $embeddedFields; |
|
30
|
|
|
|
|
31
|
|
|
protected $joins = []; |
|
32
|
|
|
|
|
33
|
|
|
protected $queryBuilderFactory; |
|
34
|
|
|
|
|
35
|
|
|
protected $queryOptions; |
|
36
|
5 |
|
|
|
37
|
|
|
protected $configProvider; |
|
38
|
5 |
|
|
|
39
|
|
|
public function __construct($manager, $class) |
|
40
|
5 |
|
{ |
|
41
|
|
|
parent::__construct($manager, $class); |
|
42
|
5 |
|
|
|
43
|
5 |
|
$this->fields = array_keys($this->getClassMetadata()->fieldMappings); |
|
44
|
5 |
|
|
|
45
|
5 |
|
$entityName = explode('\\', strtolower($this->getEntityName()) ); |
|
46
|
|
|
$entityName = $entityName[count($entityName)-1]; |
|
47
|
5 |
|
$entityAlias = $entityName[0]; |
|
48
|
5 |
|
$this->entityAlias = $entityAlias; |
|
49
|
|
|
|
|
50
|
|
|
$this->queryBuilderFactory = new QueryBuilderFactory($this->getEntityManager()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function initFromQueryBuilderOptions(QueryBuilderOptions $options) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->queryBuilderFactory->createQueryBuilder($this->getEntityName(), $this->entityAlias); |
|
56
|
|
|
|
|
57
|
|
|
$fieldMappings = $this->getClassMetadata()->fieldMappings; |
|
58
|
|
|
$this->fields = array_keys($fieldMappings); |
|
59
|
|
|
|
|
60
|
|
|
$this->queryBuilderFactory->setFields($this->fields ?? []); |
|
61
|
|
|
$this->queryBuilderFactory->setFilters($options->getFilters()); |
|
62
|
|
|
$this->queryBuilderFactory->setOrFilters($options->getOrFilters()); |
|
63
|
|
|
$this->queryBuilderFactory->setSorting($options->getSorting()); |
|
64
|
|
|
$this->queryBuilderFactory->setRel($options->getRel()); |
|
65
|
|
|
$this->queryBuilderFactory->setPrinting($options->getPrinting()); |
|
66
|
|
|
$this->queryBuilderFactory->setSelect($options->getSelect()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getQueryBuilderFactory() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->initFromQueryBuilderOptions($this->queryOptions); |
|
72
|
|
|
|
|
73
|
|
|
return $this->queryBuilderFactory; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function useResultCache($bool) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->use_result_cache = $bool; |
|
79
|
|
|
} |
|
80
|
1 |
|
|
|
81
|
|
|
public function setRequest(Request $request) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->setQueryOptionsFromRequest($request); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function setRequestWithFilter(Request $request, $filter) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->setQueryOptionsFromRequestWithCustomFilter($request, $filter); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function setRequestWithOrFilter(Request $request, $orFilter) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->setQueryOptionsFromRequestWithCustomOrFilter($request, $orFilter); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function setQueryOptions(QueryBuilderOptions $options) |
|
97
|
|
|
{ |
|
98
|
3 |
|
$this->queryOptions = $options; |
|
99
|
|
|
} |
|
100
|
3 |
|
|
|
101
|
|
|
public function setQueryOptionsFromRequest(Request $request = null) |
|
102
|
3 |
|
{ |
|
103
|
3 |
|
$requestAttributes = []; |
|
104
|
3 |
|
foreach ($request->attributes->all() as $attributeName => $attributeValue) { |
|
105
|
3 |
|
$requestAttributes[$attributeName] = $request->attributes->get( |
|
106
|
3 |
|
$attributeName, |
|
107
|
3 |
|
$attributeValue |
|
108
|
3 |
|
); |
|
109
|
3 |
|
} |
|
110
|
3 |
|
|
|
111
|
|
|
$filters = $request->query->get('filtering', []); |
|
112
|
3 |
|
$orFilters = $request->query->get('filtering_or', []); |
|
113
|
|
|
$sorting = $request->query->get('sorting', []); |
|
114
|
3 |
|
$printing = $request->query->get('printing', []); |
|
115
|
3 |
|
$rel = $request->query->get('rel', ''); |
|
116
|
|
|
$page = $request->query->get('page', ''); |
|
117
|
|
|
$select = $request->query->get('select', $this->entityAlias); |
|
118
|
|
|
$pageLength = $request->query->get('limit', 666); |
|
|
|
|
|
|
119
|
|
|
$filtering = $request->query->get('filtering', ''); |
|
120
|
|
|
$limit = $request->query->get('limit', ''); |
|
121
|
|
|
|
|
122
|
|
|
$filterOrCorrected = []; |
|
123
|
|
|
|
|
124
|
|
|
$count = 0; |
|
125
|
|
|
foreach ($orFilters as $key => $filter) { |
|
126
|
|
|
if (is_array($filter)) { |
|
127
|
3 |
|
foreach ($filter as $keyInternal => $internal) { |
|
128
|
3 |
|
$filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
|
129
|
3 |
|
$count = $count + 1; |
|
130
|
3 |
|
} |
|
131
|
3 |
|
} else { |
|
132
|
3 |
|
$filterOrCorrected[$key] = $filter; |
|
133
|
3 |
|
} |
|
134
|
3 |
|
} |
|
135
|
3 |
|
|
|
136
|
3 |
|
$requestProperties = [ |
|
137
|
|
|
'filtering' => $filtering, |
|
138
|
|
|
'orFiltering' => $filterOrCorrected, |
|
139
|
3 |
|
'limit' => $limit, |
|
140
|
3 |
|
'page' => $page, |
|
141
|
3 |
|
'filters' => $filters, |
|
142
|
|
|
'orFilters' => $filterOrCorrected, |
|
143
|
|
|
'sorting' => $sorting, |
|
144
|
3 |
|
'rel' => $rel, |
|
145
|
|
|
'printing' => $printing, |
|
146
|
3 |
|
'select' => $select, |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
4 |
|
$options = array_merge( |
|
150
|
|
|
$requestAttributes, |
|
151
|
4 |
|
$requestProperties |
|
152
|
|
|
); |
|
153
|
4 |
|
|
|
154
|
|
|
$this->queryOptions = QueryBuilderOptions::fromArray($options); |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
View Code Duplication |
public function setQueryOptionsFromRequestWithCustomFilter(Request $request = null, $filter) |
|
|
|
|
|
|
160
|
4 |
|
{ |
|
161
|
|
|
$filters = $request->query->get('filtering', []); |
|
162
|
|
|
$orFilters = $request->query->get('filtering_or', []); |
|
163
|
|
|
$sorting = $request->query->get('sorting', []); |
|
164
|
|
|
$printing = $request->query->get('printing', []); |
|
165
|
|
|
$rel = $request->query->get('rel', ''); |
|
166
|
|
|
$page = $request->query->get('page', ''); |
|
167
|
|
|
$select = $request->query->get('select', $this->entityAlias); |
|
168
|
|
|
$pageLength = $request->query->get('limit', 666); |
|
|
|
|
|
|
169
|
|
|
$filtering = $request->query->get('filtering', ''); |
|
170
|
|
|
$limit = $request->query->get('limit', ''); |
|
171
|
|
|
|
|
172
|
|
|
$filters = array_merge($filters, $filter); |
|
173
|
|
|
|
|
174
|
|
|
$filterOrCorrected = []; |
|
175
|
|
|
|
|
176
|
|
|
$count = 0; |
|
177
|
|
|
foreach ($orFilters as $key => $filter) { |
|
178
|
|
|
if (is_array($filter)) { |
|
179
|
|
|
foreach ($filter as $keyInternal => $internal) { |
|
180
|
|
|
$filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
|
181
|
|
|
$count = $count + 1; |
|
182
|
|
|
} |
|
183
|
|
|
} else { |
|
184
|
|
|
$filterOrCorrected[$key] = $filter; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->queryOptions = QueryBuilderOptions::fromArray([ |
|
189
|
|
|
'_route' => $request->attributes->get('_route'), |
|
190
|
|
|
'customer_id' => $request->attributes->get('customer_id'), |
|
191
|
|
|
'id' => $request->attributes->get('id'), |
|
192
|
|
|
'filtering' => $filtering, |
|
193
|
|
|
'limit' => $limit, |
|
194
|
|
|
'page' => $page, |
|
195
|
|
|
'filters' => $filters, |
|
196
|
|
|
'orFilters' => $filterOrCorrected, |
|
197
|
|
|
'sorting' => $sorting, |
|
198
|
|
|
'rel' => $rel, |
|
199
|
|
|
'printing' => $printing, |
|
200
|
|
|
'select' => $select, |
|
201
|
|
|
]); |
|
202
|
|
|
|
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
View Code Duplication |
public function setQueryOptionsFromRequestWithCustomOrFilter(Request $request = null, $orFilter) |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
|
|
$filters = $request->query->get('filtering', []); |
|
209
|
|
|
$orFilters = $request->query->get('filtering_or', []); |
|
210
|
|
|
$sorting = $request->query->get('sorting', []); |
|
211
|
|
|
$printing = $request->query->get('printing', []); |
|
212
|
|
|
$rel = $request->query->get('rel', ''); |
|
213
|
|
|
$page = $request->query->get('page', ''); |
|
214
|
|
|
$select = $request->query->get('select', $this->entityAlias); |
|
215
|
|
|
$pageLength = $request->query->get('limit', 666); |
|
|
|
|
|
|
216
|
|
|
$filtering = $request->query->get('filtering', ''); |
|
217
|
|
|
$limit = $request->query->get('limit', ''); |
|
218
|
1 |
|
|
|
219
|
|
|
$orFilters = array_merge($orFilters, $orFilter); |
|
220
|
1 |
|
|
|
221
|
|
|
$filterOrCorrected = []; |
|
222
|
1 |
|
|
|
223
|
1 |
|
$count = 0; |
|
224
|
1 |
|
foreach ($orFilters as $key => $filter) { |
|
225
|
1 |
|
if (is_array($filter)) { |
|
226
|
1 |
|
foreach ($filter as $keyInternal => $internal) { |
|
227
|
1 |
|
$filterOrCorrected[$keyInternal .'|' . $count] = $internal; |
|
228
|
1 |
|
$count = $count + 1; |
|
229
|
1 |
|
} |
|
230
|
1 |
|
} else { |
|
231
|
|
|
$filterOrCorrected[$key] = $filter; |
|
232
|
1 |
|
} |
|
233
|
|
|
} |
|
234
|
1 |
|
|
|
235
|
|
|
$this->queryOptions = QueryBuilderOptions::fromArray([ |
|
236
|
1 |
|
'_route' => $request->attributes->get('_route'), |
|
237
|
1 |
|
'customer_id' => $request->attributes->get('customer_id'), |
|
238
|
|
|
'id' => $request->attributes->get('id'), |
|
239
|
|
|
'filtering' => $filtering, |
|
240
|
|
|
'limit' => $limit, |
|
241
|
|
|
'page' => $page, |
|
242
|
|
|
'filters' => $filters, |
|
243
|
|
|
'orFilters' => $filterOrCorrected, |
|
244
|
|
|
'sorting' => $sorting, |
|
245
|
|
|
'rel' => $rel, |
|
246
|
|
|
'printing' => $printing, |
|
247
|
|
|
'select' => $select, |
|
248
|
|
|
]); |
|
249
|
1 |
|
|
|
250
|
1 |
|
return $this; |
|
251
|
1 |
|
} |
|
252
|
1 |
|
|
|
253
|
1 |
|
public function getRequest() |
|
254
|
1 |
|
{ |
|
255
|
1 |
|
return $this->request; |
|
256
|
1 |
|
} |
|
257
|
1 |
|
|
|
258
|
1 |
|
public function setRouteName($route_name = '') |
|
259
|
1 |
|
{ |
|
260
|
1 |
|
$this->route_name = $route_name; |
|
261
|
|
|
return $this; |
|
262
|
|
|
} |
|
263
|
1 |
|
|
|
264
|
1 |
|
public function findAllPaginated() |
|
265
|
1 |
|
{ |
|
266
|
|
|
if (!$this->queryOptions && $this->configProvider) { |
|
267
|
|
|
$this->setRequest($this->configProvider->getRequest()); |
|
268
|
1 |
|
} |
|
269
|
|
|
|
|
270
|
1 |
|
$this->initFromQueryBuilderOptions($this->queryOptions); |
|
271
|
|
|
|
|
272
|
|
|
$this->queryBuilderFactory->filter(); |
|
273
|
4 |
|
$this->queryBuilderFactory->sort(); |
|
274
|
|
|
|
|
275
|
4 |
|
return $this->paginateResults($this->queryBuilderFactory->getQueryBuilder()); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
protected function paginateResults( |
|
279
|
|
|
\Doctrine\ORM\QueryBuilder $queryBuilder |
|
280
|
|
|
) { |
|
281
|
|
|
$limit = $this->queryOptions->get('limit', 10); |
|
282
|
|
|
$page = $this->queryOptions->get('page', 1); |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
$pagerAdapter = new DoctrineORMAdapter($queryBuilder); |
|
286
|
|
|
|
|
287
|
|
|
$query = $pagerAdapter->getQuery(); |
|
288
|
|
|
if(isset($this->use_result_cache) and $this->use_result_cache){ |
|
289
|
|
|
$query->useResultCache(true, 600); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
$pager = new Pagerfanta($pagerAdapter); |
|
293
|
|
|
$pager->setNormalizeOutOfRangePages(true); |
|
294
|
|
|
$pager->setMaxPerPage($limit); |
|
295
|
|
|
$pager->setCurrentPage($page); |
|
296
|
|
|
|
|
297
|
|
|
$pagerFactory = new PagerfantaFactory(); |
|
298
|
|
|
|
|
299
|
|
|
$router = $this->createRouter(); |
|
300
|
|
|
|
|
301
|
|
|
$results = $pagerFactory->createRepresentation($pager, $router); |
|
302
|
|
|
|
|
303
|
|
|
return $results; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
protected function customQueryStringValues() |
|
307
|
|
|
{ |
|
308
|
|
|
return []; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
protected function createRouter() |
|
312
|
|
|
{ |
|
313
|
|
|
$request = $this->getRequest(); |
|
|
|
|
|
|
314
|
|
|
$params = []; |
|
315
|
|
|
|
|
316
|
|
|
$list = array_merge([ |
|
317
|
|
|
'filtering', |
|
318
|
|
|
'limit', |
|
319
|
|
|
'page', |
|
320
|
|
|
'sorting', |
|
321
|
|
|
], $this->customQueryStringValues()); |
|
322
|
|
|
|
|
323
|
|
|
foreach ($list as $itemKey => $itemValue) { |
|
324
|
|
|
$params[$itemValue] = $this->queryOptions->get($itemValue); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
if(!isset($this->route_name)){ |
|
328
|
|
|
$this->route_name = $this->queryOptions->get('_route'); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
return new Route($this->route_name, $params); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** @deprecate use QueryBuilderFatory instead */ |
|
335
|
|
|
public function noExistsJoin($prevEntityAlias, $currentEntityAlias) |
|
336
|
|
|
{ |
|
337
|
|
|
$needle = $prevEntityAlias . "_" . $currentEntityAlias; |
|
338
|
|
|
return ! in_array($needle, $this->joins); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** @deprecate use QueryBuilderFatory instead */ |
|
342
|
|
|
public function storeJoin($prevEntityAlias, $currentEntityAlias) |
|
343
|
|
|
{ |
|
344
|
|
|
$needle = $prevEntityAlias . "_" . $currentEntityAlias; |
|
345
|
|
|
$this->joins[$needle] = $needle; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** @deprecate use QueryBuilderFatory instead */ |
|
349
|
|
|
public function join($queryBuilder, $key, $val) |
|
|
|
|
|
|
350
|
|
|
{ |
|
351
|
|
|
if (strstr($key, '_embedded.')) { |
|
352
|
|
|
$embeddedFields = explode('.', $key); |
|
353
|
|
|
$numFields = count($embeddedFields); |
|
354
|
|
|
|
|
355
|
|
|
$prevEntityAlias = $this->entityAlias; // Stocksellouts |
|
356
|
|
|
$prevEntityName = $this->getEntityName(); // Stocksellouts |
|
357
|
|
|
|
|
358
|
|
|
for ($i = 1; $i < $numFields - 1; $i++) { |
|
359
|
|
|
$metadata = $this->getEntityManager()->getClassMetadata($prevEntityName); |
|
360
|
|
|
|
|
361
|
|
|
$currentRelation = $embeddedFields[$i]; |
|
362
|
|
|
|
|
363
|
|
|
if ($metadata->hasAssociation($currentRelation)) { |
|
364
|
|
|
|
|
365
|
|
|
$association = $metadata->getAssociationMapping($currentRelation); |
|
366
|
|
|
|
|
367
|
|
|
$currentEntityAlias = $this->getEntityAlias($association['targetEntity']); |
|
368
|
|
|
|
|
369
|
|
|
if ($this->noExistsJoin($prevEntityAlias, $currentRelation)) { |
|
370
|
|
|
if ($association['isOwningSide']) { |
|
371
|
|
|
$queryBuilder->join($association['targetEntity'], "$currentEntityAlias", "WITH", "$currentEntityAlias.id = " . "$prevEntityAlias.$currentRelation"); |
|
372
|
|
|
} else { |
|
373
|
|
|
$mappedBy = $association['mappedBy']; |
|
374
|
|
|
$queryBuilder->join($association['targetEntity'], "$currentEntityAlias", "WITH", "$currentEntityAlias.$mappedBy = " . "$prevEntityAlias.id"); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
$this->storeJoin($prevEntityAlias, $currentRelation); |
|
378
|
|
|
} |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
$prevEntityAlias = $currentEntityAlias; |
|
|
|
|
|
|
382
|
|
|
$prevEntityName = $association['targetEntity']; |
|
|
|
|
|
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
$this->setEmbeddedFields($embeddedFields); |
|
386
|
|
|
$this->setCurrentEntityAlias($currentEntityAlias); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
return $queryBuilder; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
protected function getCurrentEntityAlias() : string |
|
393
|
|
|
{ |
|
394
|
|
|
return $this->currentEntityAlias; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
protected function setCurrentEntityAlias(string $currentEntityAlias) |
|
398
|
|
|
{ |
|
399
|
|
|
$this->currentEntityAlias = $currentEntityAlias; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
protected function getEmbeddedFields() : array |
|
403
|
|
|
{ |
|
404
|
|
|
return $this->embeddedFields; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
protected function setEmbeddedFields(array $embeddedFields) |
|
408
|
|
|
{ |
|
409
|
|
|
$this->embeddedFields = $embeddedFields; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** @deprecate use QueryBuilderFatory component instead */ |
|
413
|
|
|
//protected function sort($queryBuilder) |
|
414
|
|
|
//{ |
|
415
|
|
|
//$request = $this->getRequest(); |
|
416
|
|
|
//$sorting = $request->query->get('sorting', array()); |
|
417
|
|
|
|
|
418
|
|
|
//foreach ($this->fields as $field) { |
|
419
|
|
|
//if (isset($sorting[$field])) { |
|
420
|
|
|
//$direction = ($sorting[$field] === 'asc') ? 'asc' : 'desc'; |
|
421
|
|
|
//$queryBuilder->addOrderBy($this->entityAlias.'.'.$field, $direction); |
|
422
|
|
|
//} |
|
423
|
|
|
//} |
|
424
|
|
|
|
|
425
|
|
|
//// &sorting[_embedded.{{relazione}}.{{campo}}={{val}} |
|
426
|
|
|
//foreach ($sorting as $sort => $val) { |
|
427
|
|
|
//if (strstr($sort, '_embedded.')) { |
|
428
|
|
|
|
|
429
|
|
|
//$queryBuilder = $this->join($queryBuilder, $sort, $val); |
|
430
|
|
|
|
|
431
|
|
|
//$currentEntityAlias = $this->getCurrentEntityAlias(); |
|
432
|
1 |
|
//$embeddedFields = $this->getEmbeddedFields(); |
|
433
|
|
|
//$numFields = count($embeddedFields); |
|
434
|
1 |
|
|
|
435
|
1 |
|
//$fieldName = $embeddedFields[$numFields - 1]; |
|
436
|
1 |
|
//$direction = ($val === 'asc') ? 'asc' : 'desc'; |
|
437
|
|
|
//$queryBuilder->addOrderBy("$currentEntityAlias." . $fieldName, $direction); |
|
438
|
|
|
//} |
|
439
|
|
|
//} |
|
440
|
|
|
|
|
441
|
|
|
//return $queryBuilder; |
|
442
|
|
|
//} |
|
443
|
|
|
|
|
444
|
|
|
public function getEntityAlias(string $entityName) : string |
|
445
|
|
|
{ |
|
446
|
|
|
$arrayEntityName = explode('\\', strtolower($entityName) ); |
|
447
|
|
|
$entityAlias = $arrayEntityName[count($arrayEntityName)-1]; |
|
448
|
|
|
return $entityAlias; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
/** @deprecate use QueryBuilderFatory component instead */ |
|
452
|
|
|
//protected function filter($queryBuilder) |
|
453
|
|
|
//{ |
|
454
|
|
|
//$request = $this->getRequest(); |
|
455
|
|
|
//$filtering = $request->query->get('filtering', array()); |
|
456
|
|
|
|
|
457
|
|
|
//foreach ($this->fields as $field) { |
|
458
|
|
|
//if (isset($filtering[$field])) { |
|
459
|
|
|
//switch ($field) { |
|
460
|
|
|
//case 'id': |
|
461
|
|
|
//case 'year': |
|
462
|
|
|
//case 'week': |
|
463
|
|
|
//$queryBuilder->andWhere($this->entityAlias.'.'.$field.' = :filter_'.$field) |
|
464
|
|
|
//->setParameter('filter_'.$field, $filtering[$field]); |
|
465
|
|
|
//break; |
|
466
|
|
|
//default: |
|
467
|
|
|
//$queryBuilder->andWhere($this->entityAlias.'.'.$field.' LIKE :filter_'.$field) |
|
468
|
|
|
//->setParameter('filter_'.$field, '%'.$filtering[$field].'%'); |
|
469
|
|
|
//} |
|
470
|
|
|
//} |
|
471
|
|
|
//} |
|
472
|
|
|
|
|
473
|
|
|
//// &filtering[_embedded.{{relazione}}.{{campo}}]={{val}} |
|
474
|
|
|
//foreach ($filtering as $filter => $val) { |
|
475
|
|
|
//if (strstr($filter, '_embedded.')) { |
|
476
|
|
|
|
|
477
|
|
|
//$queryBuilder = $this->join($queryBuilder, $filter, $val); |
|
478
|
|
|
|
|
479
|
|
|
//$currentEntityAlias = $this->getCurrentEntityAlias(); |
|
480
|
|
|
//$embeddedFields = $this->getEmbeddedFields(); |
|
481
|
|
|
//$numFields = count($embeddedFields); |
|
482
|
|
|
//$fieldName = $embeddedFields[$numFields - 1]; |
|
483
|
|
|
|
|
484
|
|
|
//$paramName = str_replace(".", "_", $filter); |
|
485
|
|
|
|
|
486
|
|
|
//switch ($fieldName) { |
|
487
|
|
|
//case 'id': |
|
488
|
|
|
//case 'codiceClienteFornitore': |
|
489
|
|
|
//case 'codiceFamily': |
|
490
|
|
|
//case 'year': |
|
491
|
|
|
//case 'week': |
|
492
|
|
|
//$queryBuilder->andWhere("$currentEntityAlias." . $fieldName . ' = :filter_' . $paramName) |
|
493
|
|
|
//->setParameter('filter_' . $paramName, $val); |
|
494
|
|
|
//break; |
|
495
|
|
|
//default : |
|
496
|
|
|
//$queryBuilder->andWhere("$currentEntityAlias." . $fieldName . ' LIKE :filter_' . $paramName) |
|
497
|
|
|
//->setParameter('filter_' . $paramName, '%' . $val . '%'); |
|
498
|
|
|
//} |
|
499
|
|
|
//} |
|
500
|
|
|
//} |
|
501
|
|
|
|
|
502
|
|
|
//return $queryBuilder; |
|
503
|
|
|
//} |
|
504
|
|
|
|
|
505
|
|
|
protected function relationship($queryBuilder) |
|
506
|
|
|
{ |
|
507
|
|
|
return $queryBuilder; |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
/** |
|
511
|
|
|
* |
|
512
|
|
|
* @param type $insertFields |
|
|
|
|
|
|
513
|
|
|
* @param type $updateFields |
|
514
|
|
|
* |
|
515
|
|
|
* USE: |
|
516
|
|
|
* |
|
517
|
|
|
* $this->getEntityManager() |
|
518
|
|
|
* ->getRepository('User') |
|
519
|
|
|
* ->onDuplicateUpdate(['column1' => 'user_reminder_1', 'column2' => 235], ['column2' => 255]); |
|
520
|
|
|
*/ |
|
521
|
|
|
public function onDuplicateUpdate($insertFields, $updateFields) |
|
522
|
|
|
{ |
|
523
|
|
|
//---CHIAVI |
|
524
|
|
|
$array_keys = array_keys($insertFields); |
|
|
|
|
|
|
525
|
|
|
$list_keys = '`' . implode('`,`', $array_keys) . '`'; |
|
526
|
|
|
|
|
527
|
|
|
//---VALORI |
|
528
|
|
|
$list_values = "'" . implode("', '", $insertFields) . "'"; |
|
|
|
|
|
|
529
|
|
|
|
|
530
|
|
|
$table = $this->getEntityManager()->getClassMetadata($this->getEntityName())->getTableName(); |
|
531
|
|
|
|
|
532
|
|
|
$sql = 'INSERT INTO '.$table; |
|
533
|
|
|
$sql .= '('. $list_keys . ') '; |
|
534
|
|
|
//$sql .= 'VALUES("'.implode('","', $insertFields).'") '; |
|
535
|
|
|
$sql .= "VALUES(". $list_values.") "; |
|
536
|
|
|
$sql .= 'ON DUPLICATE KEY UPDATE '; |
|
537
|
|
|
|
|
538
|
|
|
$c = 0; |
|
539
|
|
|
foreach($updateFields as $column => $value) { |
|
540
|
|
|
if($c>0)$sql .= ", "; |
|
541
|
|
|
$sql .= '`'.$column . "` = '". $value."'"; |
|
542
|
|
|
$c++; |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
$stmt = $this->getEntityManager()->getConnection()->prepare($sql); |
|
546
|
|
|
$stmt->execute(); |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
public function getQueryBuilderFactoryWithoutInitialization() |
|
550
|
|
|
{ |
|
551
|
|
|
return $this->queryBuilderFactory; |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
public function setConfigProvider(ConfigProvider $provider) |
|
555
|
|
|
{ |
|
556
|
|
|
$this->configProvider = $provider; |
|
557
|
|
|
|
|
558
|
|
|
return $this; |
|
559
|
|
|
} |
|
560
|
|
|
} |
|
561
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths