1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\StructureBundle\Manager; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use Starkerxp\StructureBundle\Entity\Entity; |
9
|
|
|
use Starkerxp\StructureBundle\Manager\Exception\ObjectClassNotAllowedException; |
10
|
|
|
|
11
|
|
|
abstract class AbstractManager implements ManagerInterface |
12
|
|
|
{ |
13
|
|
|
/** @var EntityManager */ |
14
|
|
|
protected $entityManager; |
15
|
|
|
|
16
|
|
|
/** @var EntityRepository */ |
17
|
|
|
protected $repository; |
18
|
|
|
|
19
|
|
|
private $modeTransactionnal = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param EntityManager $entityManager |
23
|
|
|
* @param $entity |
24
|
|
|
*/ |
25
|
|
|
public function __construct(EntityManager $entityManager, $entity) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$this->entityManager = $entityManager; |
28
|
|
|
$this->repository = $this->entityManager->getRepository($entity); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Entity $object |
33
|
|
|
* |
34
|
|
|
* @return Entity|boolean |
35
|
|
|
* |
36
|
|
|
* @throws ObjectClassNotAllowedException |
37
|
|
|
*/ |
38
|
|
|
public function insert(Entity $object) |
39
|
|
|
{ |
40
|
|
|
if (!$this->getSupport($object)) { |
41
|
|
|
throw new ObjectClassNotAllowedException(); |
42
|
|
|
} |
43
|
|
|
$object->setCreatedAt(new DateTime()); |
44
|
|
|
$this->entityManager->persist($object); |
45
|
|
|
$this->flush(); |
46
|
|
|
|
47
|
|
|
return $object; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Entity $object |
52
|
|
|
* |
53
|
|
|
* @return Entity|boolean |
54
|
|
|
* |
55
|
|
|
* @throws ObjectClassNotAllowedException |
56
|
|
|
*/ |
57
|
|
|
public function update(Entity $object) |
58
|
|
|
{ |
59
|
|
|
if (!$this->getSupport($object)) { |
60
|
|
|
throw new ObjectClassNotAllowedException(); |
61
|
|
|
} |
62
|
|
|
$object->setUpdatedAt(new DateTime()); |
63
|
|
|
$this->flush(); |
64
|
|
|
|
65
|
|
|
return $object; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Entity $object |
70
|
|
|
* @throws ObjectClassNotAllowedException |
71
|
|
|
*/ |
72
|
|
|
public function delete(Entity $object) |
73
|
|
|
{ |
74
|
|
|
if (!$this->getSupport($object)) { |
75
|
|
|
throw new ObjectClassNotAllowedException(); |
76
|
|
|
} |
77
|
|
|
$this->entityManager->remove($object); |
78
|
|
|
$this->flush(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Finds an entity by its primary key / identifier. |
83
|
|
|
* |
84
|
|
|
* @param mixed $id The identifier. |
85
|
|
|
* @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
86
|
|
|
* or NULL if no specific lock mode should be used |
87
|
|
|
* during the search. |
88
|
|
|
* @param int|null $lockVersion The lock version. |
89
|
|
|
* |
90
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
91
|
|
|
*/ |
92
|
|
|
public function find($id, $lockMode = null, $lockVersion = null) |
93
|
|
|
{ |
94
|
|
|
return $this->repository->find($id, $lockMode, $lockVersion); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Finds a single entity by a set of criteria. |
99
|
|
|
* |
100
|
|
|
* @param array $criteria |
101
|
|
|
* @param array|null $orderBy |
102
|
|
|
* |
103
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
104
|
|
|
*/ |
105
|
|
|
public function findOneBy(array $criteria, array $orderBy = null) |
106
|
|
|
{ |
107
|
|
|
return $this->repository->findOneBy($criteria, $orderBy); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Finds entities by a set of criteria. |
112
|
|
|
* |
113
|
|
|
* @param array $criteria |
114
|
|
|
* @param array|null $orderBy |
115
|
|
|
* @param int|null $limit |
116
|
|
|
* @param int|null $offset |
117
|
|
|
* |
118
|
|
|
* @return array The objects. |
119
|
|
|
*/ |
120
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
121
|
|
|
{ |
122
|
|
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Finds all entities in the repository. |
127
|
|
|
* |
128
|
|
|
* @return array The entities. |
129
|
|
|
*/ |
130
|
|
|
public function findAll() |
131
|
|
|
{ |
132
|
|
|
return $this->repository->findAll(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return EntityRepository |
137
|
|
|
*/ |
138
|
|
|
public function getRepository() |
139
|
|
|
{ |
140
|
|
|
return $this->repository; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Vide l'UnitOfWork de l'entity manager. |
145
|
|
|
*/ |
146
|
|
|
public function clear(){ |
147
|
|
|
$this->entityManager->clear(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Permet de passer en gestion des transations manuelles. (Conseillé par SensioLabs). |
152
|
|
|
*/ |
153
|
|
|
public function beginTransaction(){ |
154
|
|
|
$this->modeTransactionnal = true; |
155
|
|
|
$this->entityManager->beginTransaction(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Dans le cas d'une gestion des transactions manuelles en cas d'échec on rollback le tout. |
160
|
|
|
*/ |
161
|
|
|
public function rollback(){ |
162
|
|
|
$this->entityManager->rollback(); |
163
|
|
|
$this->entityManager->close(); |
164
|
|
|
$this->modeTransactionnal = false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Permet de gérer un flush en mode transactions manuelles. |
169
|
|
|
*/ |
170
|
|
|
private function flush(){ |
171
|
|
|
if($this->modeTransactionnal){ |
172
|
|
|
$this->entityManager->commit(); |
173
|
|
|
} |
174
|
|
|
$this->entityManager->flush(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
} |
|
|
|
|
179
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: