1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Service; |
4
|
|
|
|
5
|
|
|
use Del\Criteria\PersonCriteria; |
6
|
|
|
use Del\Entity\Person as PersonEntity; |
7
|
|
|
use Del\Repository\Person as PersonRepository; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
|
11
|
|
|
class Person |
12
|
|
|
{ |
13
|
|
|
/** @var EntityManager $em */ |
14
|
|
|
protected $em; |
15
|
|
|
|
16
|
3 |
|
public function __construct(EntityManager $em) |
|
|
|
|
17
|
|
|
{ |
18
|
3 |
|
$this->em = $em; |
19
|
3 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $data |
23
|
|
|
* @return PersonEntity |
24
|
|
|
*/ |
25
|
3 |
|
public function createFromArray(array $data) |
26
|
|
|
{ |
27
|
3 |
|
$person = new PersonEntity(); |
28
|
3 |
|
isset($data['id']) ? $person->setId($data['id']) : null; |
29
|
3 |
|
isset($data['firstname']) ? $person->setFirstname($data['firstname']) : null; |
30
|
3 |
|
isset($data['middlename']) ? $person->setMiddlename($data['middlename']) : null; |
31
|
3 |
|
isset($data['lastname']) ? $person->setLastname($data['lastname']) : null; |
32
|
3 |
|
isset($data['aka']) ? $person->setAka($data['aka']) : null; |
33
|
3 |
|
isset($data['dob']) ? $person->setDob($data['dob']) : null; |
34
|
3 |
|
isset($data['birthplace']) ? $person->setBirthplace($data['birthplace']) : null; |
35
|
3 |
|
isset($data['country']) ? $person->setCountry($data['country']) : null; |
36
|
3 |
|
return $person; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $person |
41
|
|
|
* @return PersonEntity |
42
|
|
|
*/ |
43
|
1 |
|
public function toArray(PersonEntity $person) |
44
|
|
|
{ |
45
|
|
|
$data = [ |
46
|
1 |
|
'id' => $person->getId(), |
47
|
1 |
|
'firstname' => $person->getFirstname(), |
48
|
1 |
|
'middlename' => $person->getMiddlename(), |
49
|
1 |
|
'lastname' => $person->getLastname(), |
50
|
1 |
|
'aka' => $person->getAka(), |
51
|
1 |
|
'dob' => $person->getDob(), |
52
|
1 |
|
'birthplace' => $person->getBirthplace(), |
53
|
1 |
|
'country' => $person->getCountry(), |
54
|
1 |
|
]; |
55
|
1 |
|
return $data; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param PersonEntity $person |
60
|
|
|
* @return PersonEntity |
61
|
|
|
*/ |
62
|
1 |
|
public function savePerson(PersonEntity $person) |
63
|
|
|
{ |
64
|
1 |
|
return $this->getRepository()->save($person); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param PersonEntity $person |
69
|
|
|
* @return PersonEntity |
70
|
|
|
*/ |
71
|
1 |
|
public function deletePerson(PersonEntity $person) |
72
|
|
|
{ |
73
|
1 |
|
return $this->getRepository()->delete($person); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param PersonCriteria $criteria |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function findByCriteria(PersonCriteria $criteria) |
81
|
|
|
{ |
82
|
|
|
return $this->getRepository()->findByCriteria($criteria); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return PersonRepository |
87
|
|
|
*/ |
88
|
1 |
|
protected function getRepository() |
89
|
|
|
{ |
90
|
1 |
|
return $this->em->getRepository('Del\Entity\Person'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.