|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
7
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
|
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
9
|
|
|
|
|
10
|
|
|
class DoctrineEntityRepositoryCrudService extends EntityRepository implements CrudServiceInterface |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct($entityManager, $entityClass) |
|
13
|
|
|
{ |
|
14
|
|
|
parent::__construct($entityManager, $entityManager->getClassMetadata($entityClass)); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
|
|
public function findById($id) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($this->isUuid($id)) { |
|
23
|
|
|
return $this->findOneBy(['uuid' => $id]); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return $this->find($id); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function listPaginated(int $page, int $perPage = 50): Paginator |
|
33
|
|
|
{ |
|
34
|
|
|
$queryBuilder = $this->createQueryBuilder('entity'); |
|
35
|
|
|
$queryBuilder->setFirstResult(($page - 1) * $perPage); |
|
36
|
|
|
$queryBuilder->setMaxResults($perPage); |
|
37
|
|
|
|
|
38
|
|
|
return new Paginator($queryBuilder); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function isUuid($id) |
|
42
|
|
|
{ |
|
43
|
|
|
return 1 === preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $id); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function create($entity) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->getEntityManager()->persist($entity); |
|
52
|
|
|
$this->getEntityManager()->flush($entity); |
|
53
|
|
|
|
|
54
|
|
|
return $entity; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function update($entity) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->getEntityManager()->flush($entity); |
|
63
|
|
|
|
|
64
|
|
|
return $entity; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function remove($entity) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->getEntityManager()->remove($entity); |
|
73
|
|
|
$this->getEntityManager()->flush($entity); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function listAssociationPaginated($entity, string $fieldName, int $page = 1, $perPage = 50) |
|
80
|
|
|
{ |
|
81
|
|
|
$classMetadata = $this->getEntityManager()->getClassMetadata(get_class($entity)); |
|
82
|
|
|
$targetClass = $classMetadata->getAssociationTargetClass($fieldName); |
|
83
|
|
|
|
|
84
|
|
|
$inverseFieldName = $this->getInverseFieldName($fieldName, $classMetadata); |
|
85
|
|
|
|
|
86
|
|
|
$queryBuilder = $this->getEntityManager()->createQueryBuilder(); |
|
87
|
|
|
$queryBuilder->select('association'); |
|
88
|
|
|
$queryBuilder->from($targetClass, 'association'); |
|
89
|
|
|
$queryBuilder->join('association.' . $inverseFieldName, 'entity'); |
|
90
|
|
|
$queryBuilder->where('entity = :entity'); |
|
91
|
|
|
$queryBuilder->setParameter('entity', $entity); |
|
92
|
|
|
|
|
93
|
|
|
$queryBuilder->setFirstResult(($page - 1) * $perPage); |
|
94
|
|
|
$queryBuilder->setMaxResults($perPage); |
|
95
|
|
|
|
|
96
|
|
|
$queryBuilder->getQuery(); |
|
97
|
|
|
|
|
98
|
|
|
return new Paginator($queryBuilder); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param object $entity |
|
103
|
|
|
* @param string $fieldName |
|
104
|
|
|
* @param string|int $id |
|
105
|
|
|
*/ |
|
106
|
|
|
public function addToCollection($entity, string $fieldName, $id) |
|
107
|
|
|
{ |
|
108
|
|
|
$classMetadata = $this->getEntityManager()->getClassMetadata(get_class($entity)); |
|
109
|
|
|
$targetClass = $classMetadata->getAssociationTargetClass($fieldName); |
|
110
|
|
|
|
|
111
|
|
|
$inverseFieldName = $this->getInverseFieldName($fieldName, $classMetadata); |
|
112
|
|
|
|
|
113
|
|
|
$reference = $this->getEntityManager()->getReference($targetClass, $id); |
|
114
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
115
|
|
|
$propertyAccessor->setValue($reference, $inverseFieldName, $entity); |
|
116
|
|
|
|
|
117
|
|
|
$this->getEntityManager()->flush($reference); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param object $entity |
|
122
|
|
|
* @param string $fieldName |
|
123
|
|
|
* @param string|int $id |
|
124
|
|
|
*/ |
|
125
|
|
|
public function removeFromCollection($entity, string $fieldName, $id) |
|
126
|
|
|
{ |
|
127
|
|
|
$classMetadata = $this->getEntityManager()->getClassMetadata(get_class($entity)); |
|
128
|
|
|
$targetClass = $classMetadata->getAssociationTargetClass($fieldName); |
|
129
|
|
|
|
|
130
|
|
|
$inverseFieldName = $this->getInverseFieldName($fieldName, $classMetadata); |
|
131
|
|
|
|
|
132
|
|
|
$reference = $this->getEntityManager()->getReference($targetClass, $id); |
|
133
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
134
|
|
|
$propertyAccessor->setValue($reference, $inverseFieldName, null); |
|
135
|
|
|
|
|
136
|
|
|
$this->getEntityManager()->flush($reference); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $fieldName |
|
141
|
|
|
* @param ClassMetadata $classMetadata |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getInverseFieldName(string $fieldName, ClassMetadata $classMetadata) |
|
146
|
|
|
{ |
|
147
|
|
|
$association = $classMetadata->getAssociationMapping($fieldName); |
|
148
|
|
|
if ($classMetadata->isAssociationInverseSide($fieldName)) { |
|
149
|
|
|
return $association['mappedBy']; |
|
150
|
|
|
} else { |
|
151
|
|
|
return $association['inversedBy']; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|