1 | <?php |
||
31 | abstract class Doctrine2 extends Dao |
||
32 | { |
||
33 | /** |
||
34 | * @var \Doctrine\ORM\EntityManager The entity manager |
||
35 | */ |
||
36 | protected $manager; |
||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $entityName; |
||
41 | /** |
||
42 | * @var \Doctrine\ORM\EntityRepository The entity repository |
||
43 | */ |
||
44 | protected $repository; |
||
45 | |||
46 | /** |
||
47 | * Creates a new MongoDB DAO. |
||
48 | * |
||
49 | * @param \Doctrine\ORM\EntityManager $manager The entity manager |
||
50 | * @param string $entityName The entity name |
||
51 | * @param \Psr\Log\LoggerInterface $logger A logger |
||
52 | */ |
||
53 | 4 | public function __construct(EntityManager $manager, string $entityName, \Psr\Log\LoggerInterface $logger = null) |
|
60 | |||
61 | /** |
||
62 | * Executes something in the context of the entityManager. |
||
63 | * |
||
64 | * Exceptions are caught and translated. |
||
65 | * |
||
66 | * @param Closure $cb The closure to execute, takes the entityManager |
||
67 | * @return mixed whatever the function returns, this method also returns |
||
68 | * @throws \Caridea\Dao\Exception If a database problem occurs |
||
69 | * @see \Caridea\Dao\Exception\Translator\Doctrine |
||
70 | */ |
||
71 | 2 | protected function doExecute(\Closure $cb) |
|
79 | |||
80 | /** |
||
81 | * Executes something in the context of the entityRepository. |
||
82 | * |
||
83 | * Exceptions are caught and translated. |
||
84 | * |
||
85 | * @param Closure $cb The closure to execute, takes the entityRepository |
||
86 | * @return mixed whatever the function returns, this method also returns |
||
87 | * @throws \Caridea\Dao\Exception If a database problem occurs |
||
88 | * @see \Caridea\Dao\Exception\Translator\Doctrine |
||
89 | */ |
||
90 | 2 | protected function doExecuteInRepository(\Closure $cb) |
|
98 | } |
||
99 |
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.