Symfony 2.1 Doctrine Filters (Enable / Disable)

I am currently implementing Doctrine filters in my Symfony2.1 project with the following setting:

<?php namespace Acme\Bundle\Entity; class Article { /** * @ORM\Column(type="string") */ private $status; ... } //app/config/config.yml doctrine: orm: filters: status: class: Acme\Bundle\Filter\StatusFilter enabled: false .... //src/Acme/Bundle/Filter/StatusFilter.php namespace Acme\Bundle\Filter; use Acme\Bundle\Entity\Status; class StatusFilter extends SQLFilter { public function addFilterConstraint(ClassMetadata $target, $alias) { $filter = $target->reflClass->implementsInterface('Acme\Bundle\Entity\Status')? $alias . '.status = ' . Status::PUBLISHED : ''; return $filter; } } 

Where Acme \ Bundle \ Entity \ Status is just an interface.
The code works as expected when the filter is enabled in config.yml .

The problem is that I cannot get all the articles for administration!
Is there a way to enable this filter for a specific package?
postscript I know how to enable and disable the filter with EntityManager,
I just can't find a suitable place to do this for the front-end package.

My admin section is accessible via myadmin route prefix myadmin

www.example.com/myadmin/-> admin section = disable the filter (disabled by default) www.example.com / ... → anything else = enable the filter.

+4
source share
2 answers

there is no concept of stratification at the Doctrine level. The only way I can see is to find out which controller is being used by parsing its class name (reflection, ...) during the kernel.request event or the kernel.controller event.

You can inspire yourself with this example:

https://github.com/sensio/SensioFrameworkExtraBundle/blob/master/EventListener/ParamConverterListener.php#L46

Then, if you find that your controller is in the FrontendBundle , simply disable / enable your doctrine filter.

If you prefer to use routing to determine when to disable / enable, just use the kernel.request event. You will have access to all request parameters, for example, through $event->getRequest()->attributes->get('_controller') .

+5
source

Looking at the Doctrine code, there are ways to enable and disable filters.

Once you have defined your filter in the config.yml file, you can enable / disable it in the controller or service:

 // 'status' is the unique name of the filter in the config file $this->getDoctrine()->getManager()->getFilters()->enable('status'); $this->getDoctrine()->getManager()->getFilters()->disable('status'); 

Note: this was taken from Symfony 2.3. You will need to verify this on previous versions of Symfony / Doctrine.

+11
source

All Articles