I wanted to enter the current user in the Entity Listener, but I came across ServiceCircularReferenceException.
I know that there are other issues related to this problem, and one of the solutions mentioned was to insert the entire container service into a listener that did not work.
Then I came across a seemingly duplicate question , where the provided accepted answer was to implement UserCallable. But this again gives the same exception.
Maybe this is due to my dependence on FOSUserBUndle?
How can i fix this?
An exception:
[Symfony \ Component \ dependency injection \ Exception \ ServiceCircularReferenceException] The circular link found for serving "doctrine.orm.default_entity_manager", path: "doctrine.orm.default_entity_manager → doctrine.dbal.default_connection → h nassetdb.approval_textener → security security.authentication.manager → fos_user.user_provider.username → fos_user.user_manager ".
My UserBundle service.yml entry is:
parameters:
hn_user_callable.class: Hn\UserBundle\Services\UserCallable
hn_user.callable:
class: %hn_user_callable.class%
arguments: ["@service_container"]
How to configure an object listener:
hnassetdb.approval_listener:
class: %approval_listener.class%
arguments: ['@hn_user.callable', '@logger']
tags:
- { name: doctrine.event_listener, event: onFlush }
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
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getCurrentUser()
{
$currentUser = $this->container->get('security.context')->getToken()->getUser();
if (!$currentUser instanceof User) {
throw new NoCurrentUserAvailableException();
}
return $currentUser;
}
}
The relevant part of my listener:
class ApprovalListener extends ContainerAware implements EventSubscriber {
protected $currentUser;
private $logger;
public function __construct(UserCallableInterface $userCallable, LoggerInterface $logger)
{
$this->currentUser = $userCallable->getCurrentUser();
$this->logger = $logger;
}
...
}