| 1 | <?php |
||
| 22 | class TransactionManager |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var EntityManager |
||
| 26 | */ |
||
| 27 | private $entityManager; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param EntityManager $anEntityManager |
||
| 31 | */ |
||
| 32 | public function __construct(EntityManager $anEntityManager) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | public function beginTransaction() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | public function commit() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function rollback() |
||
| 60 | } |
||
| 61 |
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.