我确定这是一个常见的问题,我需要在SonataAdmin中关闭softdeletable和类似的过滤器,直到现在我一直在做:
use Sonata\AdminBundle\Admin\Admin as BaseAdmin;
class Admin extends BaseAdmin
{
/**
* {@inheritdoc}
*/
public function configure()
{
/**
* This executes everywhere in the admin and disables softdelete for everything, if you need something cleverer this should be rethought.
*/
$filters = $this->getModelManager()->getEntityManager($this->getClass())->getFilters();
if (array_key_exists('approvable', $filters->getEnabledFilters())) {
$filters->disable('approvable');
}
if (array_key_exists('softdeleteable', $filters->getEnabledFilters())) {
$filters->disable('softdeleteable');
}
}
}
这导致了许多问题,一,它需要条件因为管理类配置两次,一次构建导航,再次构建接口,二,管理类在寒冷时实例化前端(APC可能?)缓存,非常不酷。
你打算把这个逻辑放在哪里?
答案 0 :(得分:2)
您可以使用事件监听器。例如:
服务:
filter.configurator:
class: AppBundle\Filter\Configurator
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: kernel.controller }
听众课程:
namespace AppBundle\Filter;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\AdminBundle\Controller\CoreController;
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\AdminBundle\Controller\HelperController;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
/**
* Class Configurator
*
* @author Andrey Nilov <nilov@glavweb.ru>
*/
class Configurator
{
/**
* @var Registry
*/
private $em;
/**
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* onKernelRequest
*/
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
$controllerClass = $controller[0];
$isAdminController =
$controllerClass instanceof CRUDController ||
$controllerClass instanceof CoreController ||
$controllerClass instanceof HelperController
;
if ($isAdminController) {
$this->em->getFilters()->disable('softdeleteable');
}
}
}