Symfony 2.1 Doctrine filters (enable/disable)

随声附和 提交于 2019-12-05 15:48:43

there is no notion of bundle at Doctrine level. The only way I see would be to detect which controller is used, by parsing its className (reflection, ...) during a kernel.request event, or a kernel.controller event.

You can inspire yourself from this example:

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

Then, if you detect that your controller is in FrontendBundle, just disable/enable your doctrine filter.

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

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

Once you have defined your filter in the config.yml file, you can enable/disable in a 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 would need to test this with previous versions of Symfony/Doctrine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!