1

I am trying to access a DB using the EntityManager in my event listener but I got the following error and I do not know where the problem is.

ServiceCircularReferenceException in PhpDumper.php line 508: Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection".

services.yml

testListener:
    class:    'AppBundle\Event\MyListener'
    arguments: ['@mailer','@doctrine.orm.entity_manager']
    tags:
        - { name: doctrine.event_listener, event: postPersist }

MyListener

use AppBundle\Service\Mailer;
use Doctrine\ORM\EntityManager;

class MyListener
{
  private $mailer;
  protected $em;
function __construct(Mailer $mailer ,EntityManager $em)
{
    $this->mailer = $mailer;
    $this->em = $em;
}

public function postPersist(LifecycleEventArgs $args){//.....}
}
Nathan Van Dyken
  • 342
  • 1
  • 8
  • 21
Achraf
  • 347
  • 1
  • 5
  • 17
  • 3
    It is a by product of the way Symfony defines doctrine services. The entity manager is actually dependent on all the doctrine listeners which precludes have a listener be dependent on the entity manager. Just pull the entity manager from $args and all will be well: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#listening-and-subscribing-to-lifecycle-events – Cerad Aug 30 '17 at 15:14

0 Answers0