Symfony2-使用注释的postFlush方法的自定义参数

Symfony2-使用注释的postFlush方法的自定义参数,symfony,doctrine-orm,annotations,Symfony,Doctrine Orm,Annotations,我想在建议服务的postFlush方法中使用当前用户,因此我刚刚添加了securitycontenterface$securityContext作为构造方法的参数和建议服务类的属性: use Symfony\Component\Security\Core\SecurityContextInterface; /** * @DI\Service("pro.proposals") * @DI\Tag("doctrine.event_listener", attributes = {"event"

我想在
建议
服务的
postFlush
方法中使用当前用户,因此我刚刚添加了
securitycontenterface$securityContext
作为
构造
方法的参数和
建议
服务类的属性:

use Symfony\Component\Security\Core\SecurityContextInterface;

/**
 * @DI\Service("pro.proposals")
 * @DI\Tag("doctrine.event_listener", attributes = {"event": "onFlush", "lazy": true})
 * @DI\Tag("doctrine.event_listener", attributes = {"event": "postFlush", "lazy": true})
 */
class Proposals
{
    private $doctrine;
    private $translator;
    private $templating;
    private $notifications;
    private $proposalsWithTypeChanged;
    private $securityContext;


    /**
     * @DI\InjectParams({"notifications" = @DI\Inject("pro.notifications")})
     */
    function __construct(Registry $doctrine, TranslatorInterface $translator, Notifications $notifications, Templating $templating, SecurityContextInterface $securityContext)
    {
        $this->doctrine = $doctrine;
        $this->translator = $translator;
        $this->templating = $templating;
        $this->notifications = $notifications;
        $this->securityContext = $securityContext;
    }
但这给了我一个错误:

ServiceNotFoundException:服务“pro.propositions”依赖于不存在的服务“security\u context”

我也尝试过按上面的建议传递整个容器,但都不起作用。有什么建议吗?

替换

/**
 * @DI\InjectParams({"notifications" = @DI\Inject("pro.notifications")})
 */

我认为DIExtraBundle试图根据参数的名称猜测服务名称。您必须改为显式指定服务id

如果您的安全上下文依赖于条令,您仍然可以按照另一篇文章中的说明注入容器,例如:

/**
 * @DI\Service("pro.proposals")
 */
class Test {

    /**
     * @var SecurityContextInterface
     */
    protected $securityContext;

    /**
     * @DI\InjectParams({"container" = @DI\Inject("service_container")})
     */
    function __construct( ContainerInterface $container ) {
        $this->securityContext = $container->get( 'security.context' );
    }
} 
/**
 * @DI\Service("pro.proposals")
 */
class Test {

    /**
     * @var SecurityContextInterface
     */
    protected $securityContext;

    /**
     * @DI\InjectParams({"container" = @DI\Inject("service_container")})
     */
    function __construct( ContainerInterface $container ) {
        $this->securityContext = $container->get( 'security.context' );
    }
}