1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Grid\Source; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
|
|
|
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
|
|
|
7
|
|
|
use Dtc\GridBundle\Grid\Column\GridColumn; |
8
|
|
|
|
9
|
|
|
class EntityGridSource extends AbstractDoctrineGridSource |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
|
|
|
|
13
|
|
|
* |
14
|
|
|
* @throws \Exception |
15
|
|
|
*/ |
16
|
|
|
protected function getQueryBuilder() |
17
|
|
|
{ |
18
|
|
|
if (!$this->objectManager instanceof EntityManager) { |
19
|
|
|
throw new \Exception("Expected EntityManager, instead it's: ", get_class($this->objectManager)); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$columns = $this->getColumns(); |
23
|
|
|
$fieldList = []; |
24
|
|
|
foreach ($columns as $column) { |
25
|
|
|
if ($column instanceof GridColumn && $column->isSearchable()) { |
26
|
|
|
$fieldList[$column->getField()] = true; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$qb = $this->objectManager->createQueryBuilder(); |
31
|
|
|
$orderBy = []; |
32
|
|
|
foreach ($this->orderBy as $key => $value) { |
33
|
|
|
$orderBy[] = "u.{$key} {$value}"; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$qb->add('select', 'u') |
37
|
|
|
->add('from', "{$this->objectName} u") |
38
|
|
|
->setFirstResult($this->offset) |
39
|
|
|
->setMaxResults($this->limit); |
40
|
|
|
|
41
|
|
|
if ($this->orderBy) { |
|
|
|
|
42
|
|
|
$orderByStr = implode(',', $orderBy); |
43
|
|
|
$qb->add('orderBy', $orderByStr); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($this->filter) { |
|
|
|
|
47
|
|
|
/** @var ClassMetadata $classMetaData */ |
48
|
|
|
$classMetaData = $this->getClassMetadata(); |
49
|
|
|
$classFields = $classMetaData->fieldMappings; |
50
|
|
|
|
51
|
|
|
$validFilters = array_intersect_key($this->filter, $classFields); |
52
|
|
|
|
53
|
|
|
$query = []; |
54
|
|
|
foreach ($validFilters as $key => $value) { |
55
|
|
|
if (isset($fieldList[$key])) { |
56
|
|
|
if (is_array($value)) { |
57
|
|
|
$query[] = "u.{$key} IN :{$key}"; |
58
|
|
|
} else { |
59
|
|
|
$query[] = "u.{$key} = :{$key}"; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$qb->setParameter($key, $value); |
63
|
|
|
} |
64
|
|
|
if ($query) { |
65
|
|
|
$qb->add('where', implode(' and ', $query)); |
66
|
|
|
} else { |
67
|
|
|
$starFilter = array_intersect_key($this->filter, ['*' => null]); |
68
|
|
|
if ($starFilter) { |
|
|
|
|
69
|
|
|
$value = current($starFilter); |
70
|
|
|
$starQuery = []; |
71
|
|
|
foreach (array_keys($classFields) as $key) { |
72
|
|
|
if (isset($fieldList[$key])) { |
73
|
|
|
$starQuery[] = "u.{$key} like :{$key}"; |
74
|
|
|
$qb->setParameter($key, $value); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($starQuery) { |
79
|
|
|
$star = implode(' or ', $starQuery); |
80
|
|
|
|
81
|
|
|
if ($query) { |
|
|
|
|
82
|
|
|
$qb->andWhere($star); |
83
|
|
|
} else { |
84
|
|
|
$qb->add('where', $star); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $qb; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata |
|
|
|
|
96
|
|
|
* |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
public function getClassMetadata() |
100
|
|
|
{ |
101
|
|
|
$metaFactory = $this->objectManager->getMetadataFactory(); |
102
|
|
|
$classInfo = $metaFactory->getMetadataFor($this->objectName); |
103
|
|
|
|
104
|
|
|
return $classInfo; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return mixed |
109
|
|
|
* |
110
|
|
|
* @throws \Exception |
111
|
|
|
* @throws \Doctrine\ORM\NoResultException |
112
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
113
|
|
|
*/ |
114
|
|
|
public function getCount() |
115
|
|
|
{ |
116
|
|
|
$oldOrderBy = $this->orderBy; |
117
|
|
|
$this->orderBy = []; |
118
|
|
|
$qb = $this->getQueryBuilder(); |
119
|
|
|
$this->orderBy = $oldOrderBy; |
120
|
|
|
|
121
|
|
|
$qb->add('select', 'count(u)') |
122
|
|
|
->setFirstResult(null) |
123
|
|
|
->setMaxResults(null); |
124
|
|
|
|
125
|
|
|
return $qb->getQuery() |
126
|
|
|
->getSingleScalarResult(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
* |
132
|
|
|
* @throws \Exception |
133
|
|
|
*/ |
134
|
|
|
public function getRecords() |
135
|
|
|
{ |
136
|
|
|
return $this->getQueryBuilder()->getQuery() |
137
|
|
|
->getResult(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $id |
142
|
|
|
* |
143
|
|
|
* @return mixed|null |
144
|
|
|
* |
145
|
|
|
* @throws \Exception |
146
|
|
|
*/ |
147
|
|
|
public function find($id) |
148
|
|
|
{ |
149
|
|
|
if (!$this->objectManager instanceof EntityManager) { |
150
|
|
|
throw new \Exception("Expected EntityManager, instead it's: ", get_class($this->objectManager)); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!$this->hasIdColumn()) { |
154
|
|
|
throw new \Exception('No id column found for '.$this->objectName); |
155
|
|
|
} |
156
|
|
|
$qb = $this->objectManager->createQueryBuilder(); |
157
|
|
|
$qb->from($this->objectName, 'a'); |
158
|
|
|
$qb->select('a.'.implode(',a.', $this->getClassMetadata()->getFieldNames())); |
159
|
|
|
$qb->where('a.'.$this->idColumn.' = :id')->setParameter(':id', $id); |
160
|
|
|
$result = $qb->getQuery()->execute(); |
161
|
|
|
if (isset($result[0])) { |
162
|
|
|
return $result[0]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $id |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
* |
171
|
|
|
* @throws \Exception|\Doctrine\ORM\OptimisticLockException |
172
|
|
|
*/ |
173
|
|
|
public function remove($id) |
174
|
|
|
{ |
175
|
|
|
if (!$this->hasIdColumn()) { |
176
|
|
|
throw new \Exception('No id column found for '.$this->objectName); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$repository = $this->objectManager->getRepository($this->objectName); |
180
|
|
|
$entity = $repository->find($id); |
181
|
|
|
|
182
|
|
|
if ($entity) { |
183
|
|
|
$this->objectManager->remove($entity); |
184
|
|
|
$this->objectManager->flush(); |
185
|
|
|
|
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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