1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlaygroundGame\Mapper; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\DBAL\DBALException; |
7
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
8
|
|
|
use PlaygroundGame\Options\ModuleOptions; |
9
|
|
|
|
10
|
|
|
abstract class AbstractMapper |
11
|
|
|
{ |
12
|
|
|
abstract protected function getEntityRepository(); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \Doctrine\ORM\EntityManager |
16
|
|
|
*/ |
17
|
|
|
protected $em; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Doctrine\ORM\EntityRepository |
21
|
|
|
*/ |
22
|
|
|
protected $er; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ServiceLocatorInterface |
26
|
|
|
*/ |
27
|
|
|
protected $serviceLocator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \PlaygroundGame\Options\ModuleOptions |
31
|
|
|
*/ |
32
|
|
|
protected $options; |
33
|
|
|
|
34
|
|
|
public function __construct(EntityManager $em, ModuleOptions $options, ServiceLocatorInterface $locator) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->em = $em; |
37
|
|
|
$this->options = $options; |
38
|
|
|
$this->serviceLocator = $locator; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function findById($id) |
42
|
|
|
{ |
43
|
|
|
return $this->getEntityRepository()->find($id); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function findBy($filter, $order = null, $limit = null, $offset = null) |
47
|
|
|
{ |
48
|
|
|
return $this->getEntityRepository()->findBy($filter, $order, $limit, $offset); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function findOneBy($array = array(), $sortBy = array()) |
52
|
|
|
{ |
53
|
|
|
$er = $this->getEntityRepository(); |
54
|
|
|
|
55
|
|
|
return $er->findOneBy($array, $sortBy); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function findAll() |
59
|
|
|
{ |
60
|
|
|
return $this->getEntityRepository()->findAll(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function update($entity) |
64
|
|
|
{ |
65
|
|
|
return $this->persist($entity); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function insert($entity) |
69
|
|
|
{ |
70
|
|
|
return $this->persist($entity); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function persist($entity) |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$this->em->persist($entity); |
77
|
|
|
$this->em->flush(); |
78
|
|
|
} catch (DBALException $e) { |
79
|
|
|
throw $e; |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
throw $e; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $entity; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function remove($entity) |
88
|
|
|
{ |
89
|
|
|
$this->em->remove($entity); |
90
|
|
|
$this->em->flush(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function removeAll($entities) |
94
|
|
|
{ |
95
|
|
|
foreach ($entities as $entity) { |
96
|
|
|
$this->em->remove($entity); |
97
|
|
|
} |
98
|
|
|
$this->em->flush(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.