Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "Circular reference detected for service" issue?

I'm trying to inject my repository service into EventListener but that leads me to following exception, which, with my basic knowledge of Symfony2, I have no idea how to resolve. Exception is:

ServiceCircularReferenceException in bootstrap.php.cache line 2129:

Circular reference detected for service "doctrine.orm.default_entity_manager", path: "doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection -> person.connect.listener -> tag.repository.service".

And here is how I've declared repository and listener:

tag.repository.service:
    class: Application\Bundle\PersonBundle\Entity\TagRepository
    factory: ["@doctrine", getRepository]
    arguments: [ Application\Bundle\PersonBundle\Entity\Tag ]

person.connect.listener:
    class: Application\Bundle\PersonBundle\EventListener\ConnectListener
    arguments:
        tokenStorage: "@security.token_storage"
        tagRepo: "@tag.repository.service"
    tags:
        - { name: doctrine.event_listener, event: postPersist, connection: default }

Most answers, that I've able to find, suggest injecting service container, but I really don't want do that. Is there any way to resolve this properly?

UPD: Here is the code of the listener. Everything worked fine until I've tried to inject TagRepository

class ConnectListener
{
/**
 * @var TokenStorage
 */
private $tokenStorage;

/**
 * @var TagRepository
 */
private $tagRepo;

/**
 * @param TokenStorage $tokenStorage
 * @param TagRepository $tagRepo
 */
public function __construct(TokenStorage $tokenStorage, TagRepository $tagRepo)
{
    $this->tokenStorage = $tokenStorage;
}
/**
 * @param LifecycleEventArgs $args
 * @return void
 */
public function postPersist(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();
    $entityManager = $args->getEntityManager();

    if ($entity instanceof Person) {
        $user = $this->tokenStorage->getToken()->getUser();
        $visibility = new PersonVisibility($entity, $user);
        $visibility->setVisibilityType(PersonVisibility::VT_CREATED);
        $entityManager->persist($visibility);
        $entityManager->flush();
    }
}
}
like image 430
Sergii Nester Avatar asked Sep 08 '15 20:09

Sergii Nester


People also ask

How do you fix a circular dependency problem?

To resolve circular dependencies: Then there are three strategies you can use: Look for small pieces of code that can be moved from one project to the other. Look for code that both libraries depend on and move that code into a new shared library. Combine projectA and projectB into one library.

What is circular dependency error?

When you see the circular dependency detected error displayed in your Google spreadsheet, this means that your formula is referring to a range that contains the formula itself, or in other words when the formula input, is dependent on the output.

What is a circular object reference?

A circular reference occurs when one heap variable contains a reference to a second heap variable, and the second one contains a reference back to the first. For instance, if A is an object, and somewhere in A, there is a reference to B, and within B is a reference back to A, there is a circular reference.


1 Answers

As far as TagRepository is descendant of EntityRepository try obtaining its instance in postPersist event. Like this:

// using full classname:
$tagRepo = $entityManager->getRepository("Application\Bundle\PersonBundle\Entity\TagRepository");
// alternatively:
$tagRepo = $entityManager->getRepository("ApplicationPersonBundle:Tag");
like image 70
Andrii Kucherenko Avatar answered Sep 22 '22 22:09

Andrii Kucherenko