【问题标题】:When should Doctrine filters be disabled in the SonataAdmin workflow?什么时候应该在 SonataAdmin 工作流程中禁用 Doctrine 过滤器?
【发布时间】:2016-07-08 13:55:10
【问题描述】:

我确定这是一个常见问题,我需要在 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 可能?) 缓存,这很不酷。

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

【问题讨论】:

  • 我最近没有参与这个项目,但我相信我可以通过为适用的管理员添加自定义管理控制器来实现这一点。此外,现在可以为特定实体禁用过滤器,这可能是比全局禁用它们更好的计划。
  • 是否有针对特定实体@Steve 禁用过滤器的代码示例?

标签: symfony symfony-2.1 symfony-sonata sonata-admin


【解决方案1】:

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

服务:

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');
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2012-02-05
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2010-12-30
    相关资源
    最近更新 更多