Symfony @服务错误ServiceCircularReferenceException的参数中的mailer和@twig

Symfony @服务错误ServiceCircularReferenceException的参数中的mailer和@twig,symfony,Symfony,我试图在我的服务中加入类似树枝的参数,但我总是犯同样的错误: bootstrap.php.cache行2129中的ServiceCircularReferenceException 检测到服务“条令.orm.default\u实体\u管理器”的循环引用,路径:“条令.orm.default\u实体\u管理器->条令.dbal.default\u连接->wh.participant\u侦听器->wh.participant\u通知->细枝->安全性.授权\u检查器->安全性.认证.manager-

我试图在我的服务中加入类似树枝的参数,但我总是犯同样的错误:

bootstrap.php.cache行2129中的ServiceCircularReferenceException

检测到服务“条令.orm.default\u实体\u管理器”的循环引用,路径:“条令.orm.default\u实体\u管理器->条令.dbal.default\u连接->wh.participant\u侦听器->wh.participant\u通知->细枝->安全性.授权\u检查器->安全性.认证.manager->fos\u用户.user\u提供者.username->fos\u用户.user\u管理器”`

这是我的service.yml文件

wh.participant_notification:
    class: WH\TrainingBundle\Notification\Notification
    arguments: [@mailer, @twig]

wh.participant_listener:
    class: WH\TrainingBundle\EventListener\ParticipantListener
    arguments: [@wh.participant_notification]
    tags:
        - { name: doctrine.event_listener, event: postUpdate }
        - { name: doctrine.event_listener, event: postPersist }
我的PartcIpantListenerFile

namespace WH\TrainingBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use WH\TrainingBundle\Notification\Notification;

class ParticipantListener
{


    protected $notification;

    public function __construct(Notification $notification)
    {
        $this->notification = $notification;

    }


}
只有当我在第二个服务的参数中传递@wh.participant\u通知时,这个问题才会存在

有人有想法吗


非常感谢

这条通告虽然不清楚,但应该能给你指引方向

条令实体管理器加载其侦听器,
wh.participant\u notification
。您的服务需要
twig
,而twig又需要一系列其他东西,条令实体管理器就在其中。这导致了上述异常

解决这个问题的一个办法是使用

因此,您可以将您的服务定义为:

wh.participant_notification:
  class: WH\TrainingBundle\Notification\Notification
  calls:
    - [setMailer, ["@mailer"]]
    - [setTemplating, ["@templating"]]
并将setter方法添加到通知类中

class Notification
{
  private $mailer;
  private $templating;

  public function setMailer(\Mailer $mailer)
  {
    $this->mailer = $mailer;
  }

  public function setTemplating(\Symfony\Bundle\TwigBundle\TwigEngine $templating)
  {
    $this->templating= $templating;
  }

  ...your code...

我找到了一个解决方案,虽然不漂亮,但它是有效的:

首先,我在我的服务的参数中传递服务容器

services:

wh.participant_notification:
    class: WH\TrainingBundle\Notification\Notification
    arguments: ['@service_container']

wh.participant_listener:
    class: WH\TrainingBundle\EventListener\ParticipantListener
    arguments: ['@wh.participant_notification']
    tags:
        - { name: doctrine.event_listener, event: postPersist }
然后在my Notification.php类中:

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

private $container;

public function __construct(Container $container) {

    $this->container = $container;

}

public function subscribValidation ($participant) {

    $templating = $this->container->get('templating');
    $mailer = $this->container->get('mailer');

    ... 
我无法创建受保护的var$twig,因为问题仍然存在。 我重复一遍,它只与细枝服务(或模板)。
也许另一个能找到更好的解决方案…

-[setTemplating,[“@twig”]]公共函数setTemplating(\twig\u环境$twig){$this->template=$twig;}是的,您应该添加两个setter;)嘿@Jerome我刚刚扩展了我的答案。您可能应该将
模板作为服务注入,而不是
twig
。而且
TwigEngine
可能是正确的选择,而不是
twig_环境(v2.8.4-DEV),甚至是setter注入(如本文所述)继续提出循环引用indeed,其原因就是$mailer和$templating都声明为侦听器的受保护属性。