|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
8
|
|
|
use Dontdrinkandroot\Entity\EntityInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Philip Washington Sorst <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class CrudRepository extends EntityRepository implements CrudRepositoryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
4 |
|
public function persist(object $entity, bool $flush = true): object |
|
19
|
|
|
{ |
|
20
|
4 |
|
$this->getEntityManager()->persist($entity); |
|
21
|
|
|
|
|
22
|
4 |
|
if ($flush) { |
|
23
|
4 |
|
$this->flush($entity); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
4 |
|
return $entity; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function merge(object $entity, bool $flush = false): object |
|
33
|
|
|
{ |
|
34
|
|
|
$entity = $this->getEntityManager()->merge($entity); |
|
35
|
|
|
|
|
36
|
|
|
if ($flush) { |
|
37
|
|
|
$this->flush($entity); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $entity; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
10 |
|
public function flush(object $entity = null): void |
|
47
|
|
|
{ |
|
48
|
10 |
|
$this->getEntityManager()->flush($entity); |
|
49
|
10 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function detach(object $entity): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->getEntityManager()->detach($entity); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function removeById($id, bool $flush = false): void |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var EntityInterface $entity */ |
|
65
|
2 |
|
$entity = $this->find($id); |
|
66
|
2 |
|
$this->remove($entity, $flush); |
|
|
|
|
|
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
6 |
|
public function remove(object $entity, bool $flush = false): void |
|
73
|
|
|
{ |
|
74
|
6 |
|
$this->getEntityManager()->remove($entity); |
|
75
|
|
|
|
|
76
|
6 |
|
if ($flush) { |
|
77
|
|
|
$this->flush($entity); |
|
78
|
|
|
} |
|
79
|
6 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function removeAll(bool $flush = false, bool $iterate = true): void |
|
85
|
|
|
{ |
|
86
|
4 |
|
if ($iterate) { |
|
87
|
2 |
|
$this->removeAllByIterating(); |
|
88
|
|
|
} else { |
|
89
|
2 |
|
$this->removeAllByQuery(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
if ($flush) { |
|
93
|
4 |
|
$this->flush(); |
|
94
|
|
|
} |
|
95
|
4 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function findPaginatedBy( |
|
101
|
|
|
int $page = 1, |
|
102
|
|
|
int $perPage = 10, |
|
103
|
|
|
array $criteria = [], |
|
104
|
|
|
array $orderBy = null |
|
105
|
|
|
): Paginator { |
|
106
|
2 |
|
$queryBuilder = $this->createQueryBuilder('entity'); |
|
107
|
|
|
|
|
108
|
2 |
|
foreach ($criteria as $field => $value) { |
|
109
|
2 |
|
$queryBuilder->andWhere('entity.' . $field . ' = :' . $field); |
|
110
|
2 |
|
$queryBuilder->setParameter($field, $value); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
if (null !== $orderBy) { |
|
114
|
2 |
|
foreach ($orderBy as $field => $order) { |
|
115
|
2 |
|
$queryBuilder->addOrderBy('entity.' . $field, $order); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
$queryBuilder->setFirstResult(($page - 1) * $perPage); |
|
120
|
2 |
|
$queryBuilder->setMaxResults($perPage); |
|
121
|
|
|
|
|
122
|
2 |
|
return new Paginator($queryBuilder); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
6 |
|
public function countAll(): int |
|
129
|
|
|
{ |
|
130
|
6 |
|
$queryBuilder = $this->getEntityManager()->createQueryBuilder(); |
|
131
|
|
|
$queryBuilder |
|
132
|
6 |
|
->select('count(entity)') |
|
133
|
6 |
|
->from($this->getClassName(), 'entity'); |
|
134
|
|
|
|
|
135
|
6 |
|
return $queryBuilder->getQuery()->getSingleScalarResult(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
protected function removeAllByIterating(int $batchSize = 100): void |
|
139
|
|
|
{ |
|
140
|
2 |
|
$entities = $this->findAll(); |
|
141
|
2 |
|
$count = 0; |
|
142
|
2 |
|
foreach ($entities as $entity) { |
|
143
|
2 |
|
$this->remove($entity, false); |
|
144
|
2 |
|
$count++; |
|
145
|
2 |
|
if ($count >= $batchSize) { |
|
146
|
|
|
$this->getEntityManager()->flush(); |
|
147
|
|
|
$count = 0; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
2 |
|
} |
|
151
|
|
|
|
|
152
|
2 |
|
protected function removeAllByQuery(): void |
|
153
|
|
|
{ |
|
154
|
2 |
|
$queryBuilder = $this->createQueryBuilder('entity'); |
|
155
|
2 |
|
$queryBuilder->delete(); |
|
156
|
2 |
|
$query = $queryBuilder->getQuery(); |
|
157
|
2 |
|
$query->execute(); |
|
158
|
2 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
protected function createBlankQueryBuilder(): QueryBuilder |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->getEntityManager()->createQueryBuilder(); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: