Symfony DI : Circular service reference with Doctrine event subscriber
<p>In order to refactor the code about the ticket notification systems, I created a Doctrine listener:</p>

<pre><code>final class TicketNotificationListener implements EventSubscriber
{
    /**
     * @var TicketMailer
     */
    private $mailer;

    /**
     * @var TicketSlackSender
     */
    private $slackSender;

    /**
     * @var NotificationManager
     */
    private $notificationManager;

    /**
     * We must wait the flush to send closing notification in order to
     * be sure to have the latest message of the ticket.
     *
     * @var Ticket[]|ArrayCollection
     */
    private $closedTickets;

    /**
     * @param TicketMailer        $mailer
     * @param TicketSlackSender   $slackSender
     * @param NotificationManager $notificationManager
     */
    public function __construct(TicketMailer $mailer, TicketSlackSender $slackSender, NotificationManager $notificationManager)
    {
        $this-&gt;mailer = $mailer;
        $this-&gt;slackSender = $slackSender;
        $this-&gt;notificationManager = $notificationManager;

        $this-&gt;closedTickets = new ArrayCollection();
    }

    // Stuff...
}
</code></pre>

<p>The goal is to dispatch notifications when a Ticket or a TicketMessage entity is created or updated trough mail, Slack and internal notification, using Doctrine SQL.</p>

<p>I already had a circular dependencies issue with Doctrine, so I injected the entity manager from the event args instead:</p>

<pre><code>class NotificationManager
{
    /**
     * Must be set instead of extending the EntityManagerDecorator class to avoid circular dependency.
     *
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var NotificationRepository
     */
    private $notificationRepository;

    /**
     * @var RouterInterface
     */
    private $router;

    /**
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this-&gt;router = $router;
    }

    /**
     * @param EntityManagerInterface $entityManager
     */
    public function setEntityManager(EntityManagerInterface $entityManager)
    {
        $this-&gt;entityManager = $entityManager;
        $this-&gt;notificationRepository = $this-&gt;entityManager-&gt;getRepository('AppBundle:Notification');
    }

    // Stuff...
}
</code></pre>

<p>The manager is injected form the <code>TicketNotificationListener</code></p>

<pre><code>public function postPersist(LifecycleEventArgs $args)
{
    // Must be lazy set from here to avoid circular dependency.
    $this-&gt;notificationManager-&gt;setEntityManager($args-&gt;getEntityManager());
    $entity = $args-&gt;getEntity();
}
</code></pre>

<p>The web application is working, but when I try to run a command like <code>doctrine:database:drop</code> for example, I got this:</p>

<pre><code>[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]                                                                                                                                                                                            
  Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection -&gt; mailer.ticket -&gt; twig -&gt; security.authorization_checker -&gt; security.authentication.manager -&gt; fos_user.user_provider.username_email -&gt; fos_user.user_manager".
</code></pre>

<p>But this is concerning vendor services.</p>

<p>How to solve this one? Why I have this error only on cli?</p>

<p>Thanks.</p>