duanrang2627 2014-07-17 09:53
浏览 20

如何解决ServiceCircularReferenceException?

I wanted to inject the current user into an Entity Listener but I ran into an ServiceCircularReferenceException.

I know there are other questions dealing with this issue, and one mentioned solution was to inject the entire service_container into the listener, which didn't work.

I then stumbled accross a seemingly duplicate question, where the provided accepted answer was to implement a UserCallable. But this again yields in the exact same exception.

Could it be that it is due to my dependency of the FOSUserBUndle?

How can I fix this?

The exception:

[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException] Circular reference detected for service "doctrine.orm.default_entity_manager", path: "doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> h nassetdb.approval_listener -> security.context -> security.authentication.manager -> fos_user.user_provider.username -> fos_user.user_manager".

My UserBundle service.yml entry:

parameters:
    hn_user_callable.class: Hn\UserBundle\Services\UserCallable

hn_user.callable:
    class: %hn_user_callable.class%
    arguments: ["@service_container"]

How I setup my entity listener:

hnassetdb.approval_listener:
    class: %approval_listener.class%
    arguments: ['@hn_user.callable', '@logger']
    tags:
        - { name: doctrine.event_listener, event: onFlush }

The UserCallable:

<?php

namespace Hn\UserBundle\Services;

use Hn\UserBundle\Entity\User;
use Hn\UserBundle\Exception\NoCurrentUserAvailableException;
use Symfony\Component\DependencyInjection\ContainerInterface;

class UserCallable implements UserCallableInterface
{
    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * @return User
     * @throws \Hn\UserBundle\Exception\NoCurrentUserAvailableException
     */

    public function getCurrentUser()
    {
        $currentUser = $this->container->get('security.context')->getToken()->getUser();

        if (!$currentUser instanceof User) {
            throw new NoCurrentUserAvailableException();
        }

        return $currentUser;
    }
}

Relevant part of my listener:

class ApprovalListener extends ContainerAware implements EventSubscriber {

  /**
   * @var \Hn\UserBundle\Entity\User
   */
  protected  $currentUser;
  /**
   * @var \Psr\Log\LoggerInterface
   */
  private $logger;

  public function __construct(UserCallableInterface $userCallable, LoggerInterface $logger)
  {
    $this->currentUser = $userCallable->getCurrentUser();
    $this->logger = $logger;
  }

  ...
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 linux驱动,linux应用,多线程
    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助