How to apply a Doctrine filter to a whole bundle in Symfony?
I would like to use the SoftDeleteable behavior extension for Doctrine2 in my Symfony2 application to hide deactivated users and entities related to them. This is great and simple solution for the frontend, but I would like to disable this filter for the administration section to make it possible to re-enable these entities. The admin section is located in a separate bundle.
The documentation says it is as easy to disable this filter as writing this line before querying the repository:
$em->getFilters()->disable('soft-deleteable');
Now I would like to ask if there is any way to disable this behaviour for the whole admin bundle to make unsetting this filter for every related controller action unnecessary.
Thank you.
You detect bundle name and decide, whether to enable or disable filter.
If you wonder where to perform enabling/disabling, check this answer
Tomáš's links helped me to solve the problem. Even though it might not be the best solution that's how I implemented it:
I created a PHP class :
namespace AcmeAdminBundleEventListener;
use SymfonyComponentHttpKernelEventFilterControllerEvent;
class SoftDeleteListener {
public function onKernelController(FilterControllerEvent $event) {
// Get the full name of the current controller
$controllername = $event->getRequest()->attributes->get('_controller');
$matches = array();
// Explode the name of the current controller
preg_match('/(.*)(.*)BundleController(.*)Controller::(.*)Action/', $controllername, $matches);
// preg_match should store the name of the bundle at the second index of the array
if (isset($matches[2]) && $matches[2] == "Admin") {
$controller = $event->getController();
$doctrine = $controller[0]->get('doctrine');
$em=$doctrine->getManager();
$em->getFilters()->disable('softdeleteable');
}
}
}
And added these lines to services.yml to make it listen to the kernel.controller event:
kernel.listener.admin_softdelete_listener:
class: GoldfishAdminBundleEventListenerSoftDeleteListener
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
That way I reached my goal - the softdeletable filter is disabled for the whole AdminBundle and I don't need to turn it off manually in every single controller.
链接地址: http://www.djcxy.com/p/65030.html上一篇: DoctrineExtensions SoftDeleteable的配置条目:gedmo / doctrine