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
|
|
|
/** |
20
|
|
|
* @param EntityManager $entityManager |
21
|
|
|
* @param $entity |
22
|
|
|
*/ |
23
|
|
|
public function __construct(EntityManager $entityManager, $entity) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->entityManager = $entityManager; |
26
|
|
|
$this->repository = $this->entityManager->getRepository($entity); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Entity $object |
31
|
|
|
* |
32
|
|
|
* @return Entity|boolean |
33
|
|
|
* |
34
|
|
|
* @throws ObjectClassNotAllowedException |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function insert(Entity $object) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
if (!$this->getSupport($object)) { |
39
|
|
|
throw new ObjectClassNotAllowedException(); |
40
|
|
|
} |
41
|
|
|
$object->setCreatedAt(new DateTime()); |
42
|
|
|
$this->entityManager->persist($object); |
43
|
|
|
$this->entityManager->flush(); |
44
|
|
|
|
45
|
|
|
return $object; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Entity $object |
50
|
|
|
* |
51
|
|
|
* @return Entity|boolean |
52
|
|
|
* |
53
|
|
|
* @throws ObjectClassNotAllowedException |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function update(Entity $object) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if (!$this->getSupport($object)) { |
58
|
|
|
throw new ObjectClassNotAllowedException(); |
59
|
|
|
} |
60
|
|
|
$object->setUpdatedAt(new DateTime()); |
61
|
|
|
$this->entityManager->flush(); |
62
|
|
|
|
63
|
|
|
return $object; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Entity $object |
68
|
|
|
* @throws ObjectClassNotAllowedException |
69
|
|
|
*/ |
70
|
|
|
public function delete(Entity $object) |
71
|
|
|
{ |
72
|
|
|
if (!$this->getSupport($object)) { |
73
|
|
|
throw new ObjectClassNotAllowedException(); |
74
|
|
|
} |
75
|
|
|
$this->entityManager->remove($object); |
76
|
|
|
$this->entityManager->flush(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Finds an entity by its primary key / identifier. |
81
|
|
|
* |
82
|
|
|
* @param mixed $id The identifier. |
83
|
|
|
* @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
84
|
|
|
* or NULL if no specific lock mode should be used |
85
|
|
|
* during the search. |
86
|
|
|
* @param int|null $lockVersion The lock version. |
87
|
|
|
* |
88
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
89
|
|
|
*/ |
90
|
|
|
public function find($id, $lockMode = null, $lockVersion = null) |
91
|
|
|
{ |
92
|
|
|
return $this->repository->find($id, $lockMode, $lockVersion); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Finds a single entity by a set of criteria. |
97
|
|
|
* |
98
|
|
|
* @param array $criteria |
99
|
|
|
* @param array|null $orderBy |
100
|
|
|
* |
101
|
|
|
* @return object|null The entity instance or NULL if the entity can not be found. |
102
|
|
|
*/ |
103
|
|
|
public function findOneBy(array $criteria, array $orderBy = null) |
104
|
|
|
{ |
105
|
|
|
return $this->repository->findOneBy($criteria, $orderBy); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Finds entities by a set of criteria. |
110
|
|
|
* |
111
|
|
|
* @param array $criteria |
112
|
|
|
* @param array|null $orderBy |
113
|
|
|
* @param int|null $limit |
114
|
|
|
* @param int|null $offset |
115
|
|
|
* |
116
|
|
|
* @return array The objects. |
117
|
|
|
*/ |
118
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
119
|
|
|
{ |
120
|
|
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Finds all entities in the repository. |
125
|
|
|
* |
126
|
|
|
* @return array The entities. |
127
|
|
|
*/ |
128
|
|
|
public function findAll() |
129
|
|
|
{ |
130
|
|
|
return $this->repository->findAll(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return EntityRepository |
135
|
|
|
*/ |
136
|
|
|
public function getRepository() |
137
|
|
|
{ |
138
|
|
|
return $this->repository; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
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: