1 | <?php |
||
8 | class MenuService |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private $menuNames; |
||
14 | |||
15 | /** |
||
16 | * @var DomainConfigurationInterface |
||
17 | */ |
||
18 | private $domainConfiguration; |
||
19 | |||
20 | /** |
||
21 | * @var EntityManager |
||
22 | */ |
||
23 | private $em; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $menuEntityClass; |
||
29 | |||
30 | /** |
||
31 | * @param array $menuNames |
||
32 | * @param DomainConfigurationInterface $domainConfiguration |
||
33 | * @param EntityManager $em |
||
34 | */ |
||
35 | public function __construct(array $menuNames, DomainConfigurationInterface $domainConfiguration, EntityManager $em, $menuEntityClass) |
||
42 | |||
43 | /** |
||
44 | * Make sure the menu objects exist in the database for each locale. |
||
45 | */ |
||
46 | public function makeSureMenusExist() |
||
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | private function getLocales() |
||
86 | } |
||
87 |
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.