|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Doctrine\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
use LAG\AdminBundle\Repository\RepositoryInterface; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Abstract doctrine repository |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class DoctrineRepository extends EntityRepository implements RepositoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Save an entity. |
|
17
|
|
|
* |
|
18
|
|
|
* @param $entity |
|
19
|
|
|
* |
|
20
|
|
|
* @throws LogicException |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function save($entity) |
|
23
|
|
|
{ |
|
24
|
2 |
|
$this->validateEntity($entity); |
|
25
|
|
|
$this |
|
26
|
1 |
|
->_em |
|
27
|
1 |
|
->persist($entity) |
|
28
|
|
|
; |
|
29
|
|
|
$this |
|
30
|
1 |
|
->_em |
|
31
|
1 |
|
->flush() |
|
32
|
|
|
; |
|
33
|
1 |
|
} |
|
34
|
|
|
/** |
|
35
|
|
|
* Delete an entity. |
|
36
|
|
|
* |
|
37
|
|
|
* @param $entity |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function delete($entity) |
|
40
|
|
|
{ |
|
41
|
2 |
|
$this->validateEntity($entity); |
|
42
|
|
|
$this |
|
43
|
1 |
|
->_em |
|
44
|
1 |
|
->remove($entity) |
|
45
|
|
|
; |
|
46
|
|
|
$this |
|
47
|
1 |
|
->_em |
|
48
|
1 |
|
->flush() |
|
49
|
|
|
; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null, array $options = []) |
|
56
|
|
|
{ |
|
57
|
|
|
$queryBuilder = $this->createQueryBuilder('entity'); |
|
58
|
|
|
|
|
59
|
|
|
$this->addCriteria($queryBuilder, $criteria, $options); |
|
60
|
|
|
|
|
61
|
|
|
if (null !== $orderBy) { |
|
62
|
|
|
foreach ($orderBy as $sort => $order) { |
|
63
|
|
|
$queryBuilder |
|
64
|
|
|
->addOrderBy('entity.'.$sort, $order) |
|
65
|
|
|
; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (null !== $limit) { |
|
70
|
|
|
$queryBuilder->setMaxResults($limit); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (null !== $offset) { |
|
74
|
|
|
$queryBuilder->setFirstResult($offset); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $queryBuilder |
|
78
|
|
|
->getQuery() |
|
79
|
|
|
->getResult() |
|
80
|
|
|
; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritdoc |
|
85
|
|
|
*/ |
|
86
|
|
|
public function count(array $criteria = [], array $options = []) |
|
87
|
|
|
{ |
|
88
|
|
|
$identifiers = $this |
|
89
|
|
|
->getClassMetadata() |
|
90
|
|
|
->getIdentifierColumnNames() |
|
91
|
|
|
; |
|
92
|
|
|
$pieces = []; |
|
93
|
|
|
|
|
94
|
|
|
foreach ($identifiers as $identifier) { |
|
95
|
|
|
$pieces[] = 'entity.'.$identifier; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$queryBuilder = $this |
|
99
|
|
|
->createQueryBuilder('entity') |
|
100
|
|
|
->select('count('.implode(',', $pieces).')') |
|
101
|
|
|
; |
|
102
|
|
|
$this->addCriteria($queryBuilder, $criteria, $options); |
|
103
|
|
|
|
|
104
|
|
|
return $queryBuilder |
|
105
|
|
|
->getQuery() |
|
106
|
|
|
->getSingleScalarResult() |
|
107
|
|
|
; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Add criteria values from options. |
|
112
|
|
|
* |
|
113
|
|
|
* @param QueryBuilder $queryBuilder |
|
114
|
|
|
* @param array $criteria |
|
115
|
|
|
* @param array $options |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function addCriteria(QueryBuilder $queryBuilder, array $criteria, array $options) |
|
118
|
|
|
{ |
|
119
|
|
|
foreach ($criteria as $criterion => $value) { |
|
120
|
|
|
// default values |
|
121
|
|
|
$operator = '='; |
|
122
|
|
|
|
|
123
|
|
|
if (array_key_exists($criterion, $options)) { |
|
124
|
|
|
|
|
125
|
|
|
if (array_key_exists('operator', $options[$criterion])) { |
|
126
|
|
|
$operator = $options[$criterion]['operator']; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ('LIKE' === $operator) { |
|
131
|
|
|
$value = '%'.$value.'%'; |
|
132
|
|
|
} |
|
133
|
|
|
$whereString = sprintf( |
|
134
|
|
|
'%s %s :%s', |
|
135
|
|
|
'entity.'.$criterion, |
|
136
|
|
|
$operator, |
|
137
|
|
|
$criterion.'_value' |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
$queryBuilder |
|
141
|
|
|
->andWhere($whereString) |
|
142
|
|
|
->setParameter($criterion.'_value', $value) |
|
143
|
4 |
|
; |
|
144
|
|
|
} |
|
145
|
4 |
|
} |
|
146
|
2 |
|
|
|
147
|
2 |
|
protected function validateEntity($entity) |
|
148
|
|
|
{ |
|
149
|
|
|
if ($this->_entityName !== get_class($entity)) { |
|
150
|
2 |
|
throw new LogicException( |
|
151
|
|
|
'Only instances of "'.$this->_entityName.'" can be saved or removed in this repository' |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|