|
1
|
|
|
<?php |
|
2
|
|
|
namespace PlaygroundGame\Mapper; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\ORM\EntityManager; |
|
5
|
|
|
use PlaygroundGame\Options\ModuleOptions; |
|
6
|
|
|
use PlaygroundUser\Entity\EmailVerification as Model; |
|
7
|
|
|
use Zend\Stdlib\Hydrator\HydratorInterface; |
|
8
|
|
|
use ZfcBase\EventManager\EventProvider; |
|
9
|
|
|
|
|
10
|
|
|
class Invitation |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Doctrine\ORM\EntityManager |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $em; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \PlaygroundUser\Options\ModuleOptions |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $options; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(EntityManager $em, ModuleOptions $options) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
$this->em = $em; |
|
25
|
|
|
$this->options = $options; |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function findByUser($user) |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->getEntityRepository()->findBy(array('user'=>$user)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function findByRequestKey($key) |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->getEntityRepository()->findBy(array('requestKey'=>$key)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $id |
|
|
|
|
|
|
40
|
|
|
* @return \Heineken\Entity\Invitation |
|
41
|
|
|
*/ |
|
42
|
|
|
public function findByGame($game) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getEntityRepository()->findBy(array('game'=>$game)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $id |
|
49
|
|
|
* @return \Heineken\Entity\Invitation |
|
50
|
|
|
*/ |
|
51
|
|
|
public function findById($id) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->getEntityRepository()->find($id); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function findBy($filter, $order = null, $limit = null, $offset = null) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->getEntityRepository()->findBy($filter, $order, $limit, $offset); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return \Heineken\Entity\Invitation |
|
63
|
|
|
*/ |
|
64
|
|
|
public function findOneBy($array, $sortBy = array()) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getEntityRepository()->findOneBy($array, $sortBy); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return \Heineken\Entity\Invitation |
|
71
|
|
|
*/ |
|
72
|
|
|
public function insert($entity, $tableName = null, \Zend\Stdlib\Hydrator\HydratorInterface $hydrator = null) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
return $this->persist($entity); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return \Heineken\Entity\Invitation |
|
79
|
|
|
*/ |
|
80
|
|
|
public function update($entity, $tableName = null, \Zend\Stdlib\Hydrator\HydratorInterface $hydrator = null) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
return $this->persist($entity); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function persist($entity) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->em->persist($entity); |
|
88
|
|
|
$this->em->flush(); |
|
89
|
|
|
return $entity; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function findAll() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->findBy(array(), array('createdAt' => 'DESC')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function remove($entity) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->em->remove($entity); |
|
100
|
|
|
$this->em->flush(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getEntityRepository() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->em->getRepository('PlaygroundGame\Entity\Invitation'); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
The
EntityManagermight 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
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring 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.