| 1 | <?php |
||
| 11 | abstract class BaseFilter extends AbstractType implements BaseFilterInterface |
||
| 12 | { |
||
| 13 | protected $entityManager; |
||
| 14 | protected $request; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param EntityManager $em |
||
| 18 | * @param Request $request |
||
| 19 | */ |
||
| 20 | public function __construct(EntityManager $em, Request $request) |
||
| 25 | |||
| 26 | abstract public function buildQuery(QueryBuilder $qb, array $parameters); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | public function configureOptions(OptionsResolver $resolver) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return EntityManager |
||
| 44 | */ |
||
| 45 | public function getEntityManager() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return Request |
||
| 52 | */ |
||
| 53 | public function getRequest() |
||
| 57 | } |
||
| 58 |
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.