2

我确定这是一个常见的问题,我需要在 SonataAdmin 中关闭可软删除和类似的过滤器,直到现在我一直在做:

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?)缓存上实例化前端,这很不酷。

你打算把这个逻辑放在哪里?

4

1 回答 1

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');
        }
    }
}
于 2016-07-08T05:55:10.190 回答