|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\DataProvider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
use Doctrine\ORM\Query; |
|
9
|
|
|
use LAG\AdminBundle\Repository\RepositoryInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Default data provider using generic repositories |
|
13
|
|
|
*/ |
|
14
|
|
|
class DataProvider implements DataProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var RepositoryInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $repository; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var EntityManagerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $entityManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* DataProvider constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param RepositoryInterface $repository |
|
30
|
|
|
* @param EntityManagerInterface $entityManager |
|
31
|
|
|
*/ |
|
32
|
6 |
|
public function __construct(RepositoryInterface $repository, EntityManagerInterface $entityManager) |
|
33
|
|
|
{ |
|
34
|
6 |
|
$this->repository = $repository; |
|
35
|
6 |
|
$this->entityManager = $entityManager; |
|
36
|
6 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Find entities by criteria. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $criteria |
|
42
|
|
|
* @param array $orderBy |
|
43
|
|
|
* @param int|null $limit |
|
44
|
|
|
* @param int|null $offset |
|
45
|
|
|
* |
|
46
|
|
|
* @return array|Collection |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function findBy(array $criteria = [], $orderBy = [], $limit = null, $offset = null) |
|
49
|
|
|
{ |
|
50
|
1 |
|
if ($this->repository instanceof EntityRepository) { |
|
51
|
|
|
$queryBuilder = $this |
|
52
|
|
|
->repository |
|
53
|
|
|
->createQueryBuilder('entity') |
|
54
|
|
|
; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($criteria as $criterion => $value) { |
|
57
|
|
|
$comparison = ' = '; |
|
58
|
|
|
|
|
59
|
|
|
if ($this->isWildCardValue($value)) { |
|
60
|
|
|
$comparison = ' like '; |
|
61
|
|
|
} |
|
62
|
|
|
$parameter = ':'.$criterion; |
|
63
|
|
|
|
|
64
|
|
|
$queryBuilder |
|
65
|
|
|
->andWhere('entity.'.$criterion.$comparison.$parameter) |
|
66
|
|
|
->setParameter($parameter, $value) |
|
67
|
|
|
; |
|
68
|
|
|
} |
|
69
|
|
|
$metadata = $this |
|
70
|
|
|
->entityManager |
|
71
|
|
|
->getClassMetadata($this->repository->getClassName()) |
|
72
|
|
|
; |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
foreach ($orderBy as $sort => $order) { |
|
76
|
|
|
|
|
77
|
|
|
if ($metadata->hasAssociation($sort)) { |
|
78
|
|
|
// TODO get id dynamically |
|
79
|
|
|
$queryBuilder |
|
80
|
|
|
->leftJoin('entity.'.$sort, $sort) |
|
81
|
|
|
->addSelect('COUNT('.$sort.'.id) AS HIDDEN '.$sort.'_count') |
|
82
|
|
|
->addOrderBy($sort.'_count', $order) |
|
83
|
|
|
->addGroupBy($sort.'.id') |
|
84
|
|
|
; |
|
85
|
|
|
} else { |
|
86
|
|
|
$queryBuilder |
|
87
|
|
|
->addOrderBy('entity.'.$sort, $order) |
|
88
|
|
|
; |
|
89
|
|
|
} |
|
90
|
|
|
// TODO fix bug with association |
|
91
|
|
|
} |
|
92
|
|
|
$entities = $queryBuilder |
|
93
|
|
|
->setFirstResult($offset) |
|
94
|
|
|
->setMaxResults($limit) |
|
95
|
|
|
->getQuery() |
|
96
|
|
|
->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, []) |
|
97
|
|
|
->setHint(Query::HINT_CUSTOM_TREE_WALKERS, []) |
|
98
|
|
|
->getResult() |
|
99
|
|
|
; |
|
100
|
|
|
} else { |
|
101
|
|
|
$entities = $this |
|
102
|
1 |
|
->repository |
|
103
|
1 |
|
->findBy($criteria, $orderBy, $limit, $offset) |
|
104
|
|
|
; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return $entities; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Find an entity by its unique id. |
|
112
|
|
|
* |
|
113
|
|
|
* @param $id |
|
114
|
|
|
* @return object |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function find($id) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this |
|
119
|
1 |
|
->repository |
|
120
|
1 |
|
->find($id) |
|
121
|
|
|
; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Save an entity. |
|
126
|
|
|
* |
|
127
|
|
|
* @param $entity |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function save($entity) |
|
130
|
|
|
{ |
|
131
|
|
|
$this |
|
132
|
1 |
|
->repository |
|
133
|
1 |
|
->save($entity) |
|
134
|
|
|
; |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Remove an entity. |
|
139
|
|
|
* |
|
140
|
|
|
* @param $entity |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function remove($entity) |
|
143
|
|
|
{ |
|
144
|
|
|
$this |
|
145
|
1 |
|
->repository |
|
146
|
1 |
|
->delete($entity) |
|
147
|
|
|
; |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Create a new entity. |
|
152
|
|
|
* |
|
153
|
|
|
* @return object |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function create() |
|
156
|
|
|
{ |
|
157
|
|
|
$className = $this |
|
158
|
1 |
|
->repository |
|
159
|
1 |
|
->getClassName() |
|
160
|
|
|
; |
|
161
|
|
|
|
|
162
|
1 |
|
return new $className; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Return the number of entities in the Repository. |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $criteria |
|
169
|
|
|
* @param array $options |
|
170
|
|
|
* |
|
171
|
|
|
* @return int |
|
172
|
|
|
*/ |
|
173
|
|
|
public function count(array $criteria = [], array $options = []) |
|
174
|
|
|
{ |
|
175
|
|
|
return $this |
|
176
|
|
|
->repository |
|
177
|
|
|
->count($criteria, $options) |
|
178
|
|
|
; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
private function isWildCardValue($value) |
|
182
|
|
|
{ |
|
183
|
|
|
if ('%' !== substr($value, 0, 1)) { |
|
184
|
|
|
return false; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if ('%' !== substr($value, -1, 1)) { |
|
188
|
|
|
return false; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return true; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|