2

我在 Symfony2 项目中有一个 Doctrine 过滤器。我试图在每个请求上将过滤器的参数设置为某个值(取自会话)。问题是过滤器对象是在 Symfony 的 onKernelRequest 事件之后创建的,所以我不能从那里设置它。如果我尝试在 Doctrine 的 postConnect 事件循环依赖中设置它:

ServiceCircularReferenceException: Circular reference detected for service "doctrine.orm.private_entity_manager", path: "routing.loader -> assetic.asset_manager -> twig -> translator.default -> doctrine.orm.private_entity_manager -> doctrine.dbal.private_connection -> year_visibility.parameter_setter".

问题是,我应该在哪里(或者更确切地说如何)设置过滤器的参数?

4

2 回答 2

5

您可以尝试手动定义过滤器并同时传递所需的参数。

services:
    app.filter_manager:
        class: App\Bundle\AppBundle\Filter\FilterManager
        arguments: [@doctrine.orm.entity_manager, @session]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

在过滤器管理器类中:

// ...

public function __construct(EntityManager $em, SessionInterface $session) 
{
    $this->em = $em;
    $this->session = $session;
}

// ...

public function onKernelRequest()
{
    $this->em->getConfiguration()->addFilter('filter_name', 'Filter/Class/Name/With/Ns');

    $filter = $this->em->getFilters()->enable('filter_name');
    $filter->setParameter('param_name', $this->session->get('param_name'));
}  
于 2013-04-03T15:54:00.040 回答
2

如此处所示:https ://stackoverflow.com/a/14650403/244058 ,

您可以在内核启动时拥有 Filter 类的实例。因此,您的实例将早就可用。

<?php

class MyBundle extends Bundle 
{
    public function boot()
    {
        $em = $this->container->get('doctrine.orm.default_entity_manager');
        $conf = $em->getConfiguration();
        $conf->addFilter(
            'filter_name',
            'Doctrine\Filter\TestFilter'
        );

        // either enable it here, or later in the event listener
        $em->getFilters()->enable('filter_name');
    }
}

之后,只需添加一个 kernel.event_listener 来监听kernel.request并设置一个过滤器参数(类似这样):

<?php

class DoctrineSqlFilterConfigurator
{
    private $em; // inject the entity manager somehow (ctor is a good idea)

    public function onKernelRequest(GetResponseEvent $event)
    {
        $filter = $this->em->getFilters()->enable('filter_name');
        $filter->setParameter('param_name', $event->getRequest()->getSession()->get('param_name'));
    }
}
于 2013-04-04T11:25:14.023 回答