-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathEntity.php
More file actions
892 lines (745 loc) · 28.5 KB
/
Entity.php
File metadata and controls
892 lines (745 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<?php
/*
* This file is part of the DataGridBundle.
*
* (c) Abhoryo <abhoryo@free.fr>
* (c) Stanislav Turza
* (c) Patryk Grudniewski <patgrudniewski@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace APY\DataGridBundle\Grid\Source;
use APY\DataGridBundle\Grid\Column\Column;
use APY\DataGridBundle\Grid\Column\JoinColumn;
use APY\DataGridBundle\Grid\Row;
use APY\DataGridBundle\Grid\Rows;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Tools\Pagination\CountWalker;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpKernel\Kernel;
class Entity extends Source
{
const DOT_DQL_ALIAS_PH = '__dot__';
const COLON_DQL_ALIAS_PH = '__col__';
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $manager;
/**
* @var \Doctrine\ORM\QueryBuilder
*/
protected $query;
/**
* @var \Doctrine\ORM\QueryBuilder
*/
protected $querySelectfromSource;
/**
* @var string e.g Vendor\Bundle\Entity\Page
*/
protected $class;
/**
* @var string e.g Cms:Page
*/
protected $entityName;
/**
* @var string e.g mydatabase
*/
protected $managerName;
/**
* @var \APY\DataGridBundle\Grid\Mapping\Metadata\Metadata
*/
protected $metadata;
/**
* @var \Doctrine\ORM\Mapping\ClassMetadata
*/
protected $ormMetadata;
/**
* @var array
*/
protected $joins;
/**
* @var string
*/
protected $group;
/**
* @var string
*/
protected $groupBy;
/**
* @var array
*/
protected $hints;
/**
* The QueryBuilder that will be used to start generating query for the DataGrid
* You can override this if the querybuilder is constructed in a business-specific way
* by an external controller/service/repository and you wish to re-use it for the datagrid.
* Typical use-case involves an external repository creating complex default restriction (i.e. multi-tenancy etc)
* which then will be expanded on by the datagrid.
*
* @var QueryBuilder
*/
protected $queryBuilder;
/**
* The table alias that will be used in the query to fetch actual data.
*
* @var string
*/
protected $tableAlias;
/**
* @var null
*/
protected $prepareCountQueryCallback = null;
/**
* Legacy way of accessing the default alias (before it became possible to change it)
* Please use $entity->getTableAlias() now instead of $entity::TABLE_ALIAS.
*
* @deprecated
*/
const TABLE_ALIAS = '_a';
/**
* @param string $entityName e.g Cms:Page
* @param string $managerName e.g. mydatabase
*/
public function __construct($entityName, $group = 'default', $managerName = null)
{
$this->entityName = $entityName;
$this->managerName = $managerName;
$this->joins = [];
$this->group = $group;
$this->hints = [];
$this->setTableAlias(self::TABLE_ALIAS);
}
public function initialise($container)
{
$doctrine = $container->get('doctrine');
$this->manager = version_compare(Kernel::VERSION, '2.1.0', '>=') ? $doctrine->getManager($this->managerName) : $doctrine->getEntityManager($this->managerName);
$this->ormMetadata = $this->manager->getClassMetadata($this->entityName);
$this->class = $this->ormMetadata->getReflectionClass()->name;
$mapping = $container->get('grid.mapping.manager');
/* todo autoregister mapping drivers with tag */
$mapping->addDriver($this, -1);
$this->metadata = $mapping->getMetadata($this->class, $this->group);
$this->groupBy = $this->metadata->getGroupBy();
}
/**
* @param \APY\DataGridBundle\Grid\Column\Column $column
*
* @return string
*/
protected function getTranslationFieldNameWithParents($column)
{
$name = $column->getField();
if ($column->getIsManualField()) {
return $column->getField();
}
if (strpos($name, '.') !== false) {
$previousParent = '';
$elements = explode('.', $name);
while ($element = array_shift($elements)) {
if (count($elements) > 0) {
$previousParent .= '_' . $element;
}
}
} elseif (strpos($name, ':') !== false) {
$previousParent = $this->getTableAlias();
} else {
return $this->getTableAlias().'.'.$name;
}
$matches = array();
if ($column->hasDQLFunction($matches)) {
return $previousParent.'.'.$matches['field'];
}
return $column->getField();
}
/**
* @param \APY\DataGridBundle\Grid\Column\Column $column
* @return string
*/
protected function getFieldName($column, $withAlias = false)
{
$name = $column->getField();
if ($column->getIsManualField()) {
return $column->getField();
}
if (strpos($name, '.') !== false) {
$previousParent = '';
$elements = explode('.', $name);
while ($element = array_shift($elements)) {
if (count($elements) > 0) {
$parent = ($previousParent == '') ? $this->getTableAlias() : $previousParent;
$previousParent .= '_' . $element;
$this->joins[$previousParent] = ['field' => $parent . '.' . $element, 'type' => $column->getJoinType()];
} else {
$name = $previousParent . '.' . $element;
}
}
$alias = $this->fromColIdToAlias($column->getId());
} elseif (strpos($name, ':') !== false) {
$previousParent = $this->getTableAlias();
$alias = $name;
} else {
return $this->getTableAlias() . '.' . $name;
}
// Aggregate dql functions
$matches = [];
if ($column->hasDQLFunction($matches)) {
if (strtolower($matches['parameters']) == 'distinct') {
$functionWithParameters = $matches['function'] . '(DISTINCT ' . $previousParent . '.' . $matches['field'] . ')';
} else {
$parameters = '';
if ($matches['parameters'] !== '') {
$parameters = ', ' . (is_numeric($matches['parameters']) ? $matches['parameters'] : "'" . $matches['parameters'] . "'");
}
$functionWithParameters = $matches['function'] . '(' . $previousParent . '.' . $matches['field'] . $parameters . ')';
}
if ($withAlias) {
// Group by the primary field of the previous entity
$this->query->addGroupBy($previousParent);
$this->querySelectfromSource->addGroupBy($previousParent);
return "$functionWithParameters as $alias";
}
// To handle query from filter with necessary HAVING clause which does not accept alias
if (!$withAlias && $hasHaving) {
return $functionWithParameters;
}
return $alias;
}
if ($withAlias) {
return "$name as $alias";
}
return $name;
}
/**
* @param string $colId
*
* @return string
*/
private function fromColIdToAlias($colId)
{
return str_replace(['.', ':'], [self::DOT_DQL_ALIAS_PH, self::COLON_DQL_ALIAS_PH], $colId);
}
/**
* @param string $fieldName
*
* @return string
*/
protected function getGroupByFieldName($fieldName)
{
if (strpos($fieldName, '.') !== false) {
$previousParent = '';
$elements = explode('.', $fieldName);
while ($element = array_shift($elements)) {
if (count($elements) > 0) {
$previousParent .= '_' . $element;
} else {
$name = $previousParent . '.' . $element;
}
}
} else {
if (($pos = strpos($fieldName, ':')) !== false) {
$fieldName = substr($fieldName, 0, $pos);
}
return $this->getTableAlias() . '.' . $fieldName;
}
return $name;
}
/**
* @param \APY\DataGridBundle\Grid\Columns $columns
*/
public function getColumns($columns)
{
foreach ($this->metadata->getColumnsFromMapping($columns) as $column) {
$columns->addColumn($column);
}
}
protected function normalizeOperator($operator)
{
switch ($operator) {
//case Column::OPERATOR_REGEXP:
case Column::OPERATOR_LIKE:
case Column::OPERATOR_LLIKE:
case Column::OPERATOR_RLIKE:
case Column::OPERATOR_NLIKE:
case Column::OPERATOR_SLIKE:
case Column::OPERATOR_LSLIKE:
case Column::OPERATOR_RSLIKE:
case Column::OPERATOR_NSLIKE:
return 'like';
default:
return $operator;
}
}
protected function normalizeValue($operator, $value)
{
switch ($operator) {
//case Column::OPERATOR_REGEXP:
case Column::OPERATOR_LIKE:
case Column::OPERATOR_NLIKE:
case Column::OPERATOR_SLIKE:
case Column::OPERATOR_NSLIKE:
return "%$value%";
case Column::OPERATOR_LLIKE:
case Column::OPERATOR_LSLIKE:
return "%$value";
case Column::OPERATOR_RLIKE:
case Column::OPERATOR_RSLIKE:
return "$value%";
default:
return $value;
}
}
/**
* Sets the initial QueryBuilder for this DataGrid.
*
* @param QueryBuilder $queryBuilder
*/
public function initQueryBuilder(QueryBuilder $queryBuilder)
{
$this->queryBuilder = clone $queryBuilder;
//Try to guess the new root alias and apply it to our queries+
//as the external querybuilder almost certainly is not used our default alias
$externalTableAliases = $this->queryBuilder->getRootAliases();
if (count($externalTableAliases)) {
$this->setTableAlias($externalTableAliases[0]);
}
}
/**
* @return QueryBuilder
*/
protected function getQueryBuilder()
{
//If a custom QB has been provided, use a copy of that one
//Otherwise create our own basic one
if ($this->queryBuilder instanceof QueryBuilder) {
$qb = clone $this->queryBuilder;
} else {
$qb = $this->manager->createQueryBuilder($this->class);
$qb->from($this->class, $this->getTableAlias());
}
return $qb;
}
/**
* @param \APY\DataGridBundle\Grid\Column\Column[] $columns
* @param int $page Page Number
* @param int $limit Rows Per Page
* @param int $gridDataJunction Grid data junction
*
* @return \APY\DataGridBundle\Grid\Rows
*/
public function execute($columns, $page = 0, $limit = 0, $maxResults = null, $gridDataJunction = Column::DATA_CONJUNCTION)
{
$this->query = $this->getQueryBuilder();
$this->querySelectfromSource = clone $this->query;
$bindIndex = 123;
$serializeColumns = [];
$where = $gridDataJunction === Column::DATA_CONJUNCTION ? $this->query->expr()->andx() : $this->query->expr()->orx();
$columnsById = [];
foreach ($columns as $column) {
$columnsById[$column->getId()] = $column;
}
foreach ($columns as $column) {
// If a column is a manual field, ie a.col*b.col as myfield, it is added to select from user.
if ($column->getIsManualField() === false) {
$fieldName = $this->getFieldName($column, true);
$this->query->addSelect($fieldName);
$this->querySelectfromSource->addSelect($fieldName);
}
if ($column->isSorted()) {
if ($column instanceof JoinColumn) {
$this->query->resetDQLPart('orderBy');
foreach ($column->getJoinColumns() as $columnName) {
$this->query->addOrderBy($this->getFieldName($columnsById[$columnName]), $column->getOrder());
}
} else {
$this->query->orderBy($this->getFieldName($column), $column->getOrder());
}
}
if ($column->isFiltered()) {
// Some attributes of the column can be changed in this function
$filters = $column->getFilters('entity');
$isDisjunction = $column->getDataJunction() === Column::DATA_DISJUNCTION;
$dqlMatches = [];
$hasHavingClause = $column->hasDQLFunction($dqlMatches) || $column->getIsAggregate();
if(isset($dqlMatches['function']) && $dqlMatches['function'] == 'translation_agg'){
$hasHavingClause = false;
}
$sub = $isDisjunction ? $this->query->expr()->orx() : ($hasHavingClause ? $this->query->expr()->andx() : $where);
foreach ($filters as $filter) {
$operator = $this->normalizeOperator($filter->getOperator());
$columnForFilter = (!$column instanceof JoinColumn) ? $column : $columnsById[$filter->getColumnName()];
$fieldName = $this->getFieldName($columnForFilter, false, $hasHavingClause);
$bindIndexPlaceholder = "?$bindIndex";
if( in_array($filter->getOperator(), array(Column::OPERATOR_LIKE,Column::OPERATOR_RLIKE,Column::OPERATOR_LLIKE,Column::OPERATOR_NLIKE,))){
if(isset($dqlMatches['function']) && $dqlMatches['function'] == 'translation_agg'){
$translationFieldName = $this->getTranslationFieldNameWithParents($columnForFilter);
$fieldName = "LOWER(".$translationFieldName.")";
}elseif(isset($dqlMatches['function']) && $dqlMatches['function'] == 'role_agg'){
$translationFieldName = $this->getTranslationFieldNameWithParents($columnForFilter);
$fieldName = "LOWER(".$translationFieldName.")";
}else{
$fieldName = "LOWER($fieldName)";
}
$bindIndexPlaceholder = "LOWER($bindIndexPlaceholder)";
}
$q = $this->query->expr()->$operator($fieldName, $bindIndexPlaceholder);
if ($filter->getOperator() == Column::OPERATOR_NLIKE || $filter->getOperator() == Column::OPERATOR_NSLIKE) {
$q = $this->query->expr()->not($q);
}
$sub->add($q);
if ($filter->getValue() !== null) {
$this->query->setParameter($bindIndex++, $this->normalizeValue($filter->getOperator(), $filter->getValue()));
}
}
if ($hasHavingClause) {
$this->query->andHaving($sub);
} elseif ($isDisjunction) {
$where->add($sub);
}
}
if ($column->getType() === 'array') {
$serializeColumns[] = $column->getId();
}
}
if ($where->count() > 0) {
//Using ->andWhere here to make sure we preserve any other where clauses present in the query builder
//the other where clauses may have come from an external builder
$this->query->andWhere($where);
}
foreach ($this->joins as $alias => $field) {
if (null !== $field['type'] && strtolower($field['type']) === 'inner') {
$join = 'join';
} else {
$join = 'leftJoin';
}
$this->query->$join($field['field'], $alias);
$this->querySelectfromSource->$join($field['field'], $alias);
}
if ($page > 0) {
$this->query->setFirstResult($page * $limit);
}
if ($limit > 0) {
if ($maxResults !== null && ($maxResults - $page * $limit < $limit)) {
$limit = $maxResults - $page * $limit;
}
$this->query->setMaxResults($limit);
} elseif ($maxResults !== null) {
$this->query->setMaxResults($maxResults);
}
if (!empty($this->groupBy)) {
$this->query->resetDQLPart('groupBy');
$this->querySelectfromSource->resetDQLPart('groupBy');
foreach ($this->groupBy as $field) {
$this->query->addGroupBy($this->getGroupByFieldName($field));
$this->querySelectfromSource->addGroupBy($this->getGroupByFieldName($field));
}
}
//call overridden prepareQuery or associated closure
$this->prepareQuery($this->query);
$hasJoin = $this->checkIfQueryHasFetchJoin($this->query);
$query = $this->query->getQuery();
foreach ($this->hints as $hintKey => $hintValue) {
$query->setHint($hintKey, $hintValue);
}
$items = new Paginator($query, $hasJoin);
$items->setUseOutputWalkers(false);
$repository = $this->manager->getRepository($this->entityName);
// Force the primary field to get the entity in the manipulatorRow
$primaryColumnId = null;
foreach ($columns as $column) {
if ($column->isPrimary()) {
$primaryColumnId = $column->getId();
break;
}
}
// hydrate result
$result = new Rows();
foreach ($items as $item) {
$row = new Row();
foreach ($item as $key => $value) {
$key = $this->fromAliasToColId($key);
if (in_array($key, $serializeColumns) && is_string($value)) {
$value = unserialize($value);
}
$row->setField($key, $value);
}
$row->setPrimaryField($primaryColumnId);
//Setting the representative repository for entity retrieving
$row->setRepository($repository);
//call overridden prepareRow or associated closure
if (($modifiedRow = $this->prepareRow($row)) !== null) {
$result->addRow($modifiedRow);
}
}
return $result;
}
/**
* @param string $alias
*
* @return string
*/
private function fromAliasToColId($alias)
{
return str_replace([self::DOT_DQL_ALIAS_PH, self::COLON_DQL_ALIAS_PH], ['.', ':'], $alias);
}
public function getTotalCount($maxResults = null)
{
// Doctrine Bug Workaround: http://www.doctrine-project.org/jira/browse/DDC-1927
$countQueryBuilder = clone $this->query;
$this->prepareCountQuery($countQueryBuilder);
foreach ($countQueryBuilder->getRootAliases() as $alias) {
$countQueryBuilder->addSelect($alias);
}
// From Doctrine\ORM\Tools\Pagination\Paginator::count()
$countQuery = $countQueryBuilder->getQuery();
// Add hints from main query, if developer wants to use additional hints (ex. gedmo translations):
foreach ($this->hints as $hintName => $hintValue) {
$countQuery->setHint($hintName, $hintValue);
}
if (!$countQuery->getHint(CountWalker::HINT_DISTINCT)) {
$countQuery->setHint(CountWalker::HINT_DISTINCT, true);
}
if ($countQuery->getHint(Query::HINT_CUSTOM_OUTPUT_WALKER) === false) {
$platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); // law of demeter win
$rsm = new ResultSetMapping();
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');
$countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Tools\Pagination\CountOutputWalker');
$countQuery->setResultSetMapping($rsm);
} else {
$hints = $countQuery->getHint(Query::HINT_CUSTOM_TREE_WALKERS);
if ($hints === false) {
$hints = [];
}
$hints[] = 'Doctrine\ORM\Tools\Pagination\CountWalker';
//$hints[] = 'APY\DataGridBundle\Grid\Helper\ORMCountWalker';
$countQuery->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints);
}
$countQuery->setFirstResult(null)->setMaxResults($maxResults);
try {
$data = $countQuery->getScalarResult();
$data = array_map('current', $data);
$count = array_sum($data);
} catch (NoResultException $e) {
$count = 0;
}
return $count;
}
public function getFieldsMetadata($class, $group = 'default')
{
$result = [];
foreach ($this->ormMetadata->getFieldNames() as $name) {
$mapping = $this->ormMetadata->getFieldMapping($name);
$values = ['title' => $name, 'source' => true];
if (isset($mapping['fieldName'])) {
$values['field'] = $mapping['fieldName'];
$values['id'] = $mapping['fieldName'];
}
if (isset($mapping['id']) && $mapping['id'] == 'id') {
$values['primary'] = true;
}
switch ($mapping['type']) {
case 'string':
case 'text':
$values['type'] = 'text';
break;
case 'integer':
case 'smallint':
case 'bigint':
case 'float':
case 'decimal':
$values['type'] = 'number';
break;
case 'boolean':
$values['type'] = 'boolean';
break;
case 'date':
$values['type'] = 'date';
break;
case 'datetime':
$values['type'] = 'datetime';
break;
case 'time':
$values['type'] = 'time';
break;
case 'array':
case 'object':
$values['type'] = 'array';
break;
}
$result[$name] = $values;
}
return $result;
}
public function populateSelectFilters($columns, $loop = false)
{
/* @var $column Column */
foreach ($columns as $column) {
$selectFrom = $column->getSelectFrom();
if ($column->getFilterType() === 'select' && ($selectFrom === 'source' || $selectFrom === 'query')) {
// For negative operators, show all values
if ($selectFrom === 'query') {
foreach ($column->getFilters('entity') as $filter) {
if (in_array($filter->getOperator(), [Column::OPERATOR_NEQ, Column::OPERATOR_NLIKE, Column::OPERATOR_NSLIKE])) {
$selectFrom = 'source';
break;
}
}
}
// Dynamic from query or not ?
$query = ($selectFrom === 'source') ? clone $this->querySelectfromSource : clone $this->query;
$query = $query->select($this->getFieldName($column, true))
->distinct()
->orderBy($this->getFieldName($column), 'asc')
->setFirstResult(null)
->setMaxResults(null)
->getQuery();
if ($selectFrom === 'query') {
foreach ($this->hints as $hintKey => $hintValue) {
$query->setHint($hintKey, $hintValue);
}
}
$result = $query->getResult();
$values = [];
foreach ($result as $row) {
$alias = $this->fromColIdToAlias($column->getId());
$value = $row[$alias];
switch ($column->getType()) {
case 'array':
if (is_string($value)) {
$value = unserialize($value);
}
foreach ($value as $val) {
$values[$val] = $val;
}
break;
case 'simple_array':
if (is_string($value)) {
$value = explode(',', $value);
}
foreach ($value as $val) {
$values[$val] = $val;
}
break;
case 'number':
$values[$value] = $column->getDisplayedValue($value);
break;
case 'datetime':
case 'date':
case 'time':
$displayedValue = $column->getDisplayedValue($value);
$values[$displayedValue] = $displayedValue;
break;
default:
$values[$value] = $value;
}
}
// It avoids to have no result when the other columns are filtered
if ($selectFrom === 'query' && empty($values) && $loop === false) {
$column->setSelectFrom('source');
$this->populateSelectFilters($columns, true);
} else {
if ($column->getType() == 'array') {
natcasesort($values);
}
$values = $this->prepareColumnValues($column, $values);
$column->setValues($values);
}
}
}
}
/**
* @param QueryBuilder $countQueryBuilder
*/
public function prepareCountQuery(QueryBuilder $countQueryBuilder)
{
if (is_callable($this->prepareCountQueryCallback)) {
call_user_func($this->prepareCountQueryCallback, $countQueryBuilder);
}
}
/**
* @param callable $callback
*
* @return $this
*/
public function manipulateCountQuery($callback = null)
{
$this->prepareCountQueryCallback = $callback;
return $this;
}
public function delete(array $ids)
{
$repository = $this->getRepository();
foreach ($ids as $id) {
$object = $repository->find($id);
if (!$object) {
throw new \Exception(sprintf('No %s found for id %s', $this->entityName, $id));
}
$this->manager->remove($object);
}
$this->manager->flush();
}
public function getRepository()
{
return $this->manager->getRepository($this->entityName);
}
public function getHash()
{
return $this->entityName;
}
public function addHint($key, $value)
{
$this->hints[$key] = $value;
}
public function clearHints()
{
$this->hints = [];
}
/**
* Set groupby column.
*
* @param string $groupBy GroupBy column
*/
public function setGroupBy($groupBy)
{
$this->groupBy = $groupBy;
}
public function getEntityName()
{
return $this->entityName;
}
/**
* @param string $tableAlias
*/
public function setTableAlias($tableAlias)
{
$this->tableAlias = $tableAlias;
}
/**
* @return string
*/
public function getTableAlias()
{
return $this->tableAlias;
}
/**
* @param QueryBuilder $qb
* @return boolean
*/
protected function checkIfQueryHasFetchJoin(QueryBuilder $qb)
{
$join = $qb->getDqlPart('join');
if (empty($join)) {
return false;
}
foreach ($join[$this->getTableAlias()] as $join) {
if ($join->getJoinType() === Join::INNER_JOIN || $join->getJoinType() === Join::LEFT_JOIN) {
return true;
}
}
return false;
}
}