什么时候应该在SonataAdmin工作流程中禁用Doctrine过滤器?

我确信这是一个常见的问题,我需要在SonataAdmin中使用softdeletable和类似的过滤器,直到现在我一直在做:

use SonataAdminBundleAdminAdmin 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可能?)缓存上实例化前端,这很不起眼。

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


您可以使用事件监听器。 例如:

服务:

filter.configurator:
class: AppBundleFilterConfigurator
arguments: ["@doctrine.orm.entity_manager"]
tags:
    - { name: kernel.event_listener, event: kernel.controller }

听众类:

namespace AppBundleFilter;

use DoctrineBundleDoctrineBundleRegistry;
use DoctrineORMEntityManagerInterface;
use SonataAdminBundleControllerCoreController;
use SonataAdminBundleControllerCRUDController;
use SonataAdminBundleControllerHelperController;
use SymfonyComponentHttpKernelEventFilterControllerEvent;

/**
 * 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');
        }
    }
}
链接地址: http://www.djcxy.com/p/65025.html

上一篇: When should Doctrine filters be disabled in the SonataAdmin workflow?

下一篇: TkInter Frame doesn't load if another function is called