I am working on a complex Symfony project that blends Doctrine ORM objects with Doctrine PHPCR-ODM documents. Everything works fine, but I have been unable to solve circular dependency injection problems between listeners in the container.
The scenario is, I have multiple ODM documents that set ORM references as they are loaded, which is accomplished via an event listener. An example configuration is:
services.yml
:
example.event_listener.my_document:
class: Example\Common\EventListener\MyDocumentEventListener
arguments: [@doctrine]
tags:
- { name: doctrine_phpcr.event_listener, event: postLoad }
- { name: doctrine_phpcr.event_listener, event: prePersist }
Example\Common\EventListener\MyDocumentEventListener.php
:
namespace Example\Common\EventListener;
use Example\Common\ODM\Document\MyDocument;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ODM\PHPCR\DocumentManager;
/**
* Listener for {@link Example\Common\ODM\Document\MyDocument} events.
*/
class MyDocumentEventListener
{
/*
* @var Doctrine\Common\Persistence\ManagerRegistry
*/
private $managerRegistry;
/**
* Constructor.
*
* @param Doctrine\Common\Persistence\ManagerRegistry $documentManager A Doctrine {@link Doctrine\Common\Persistence\ManagerRegistry}.
*/
public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
}
/**
* After loading a document, ensure that the references exist
* to each ORM dependency.
*
* @param Doctrine\Common\Persistence\Event\LifecycleEventArgs $args
*/
public function postLoad(LifecycleEventArgs $args)
{
if (get_class($args->getObject()) == 'Example\Common\ODM\Document\MyDocument') {
$this->loadDependencies($args->getObject(), $args->getObjectManager());
}
}
/**
* Prior to persisting a document, ensure that the references exist
* to each ORM dependency.
*
* @param Doctrine\Common\Persistence\Event\LifecycleEventArgs $args
*/
public function prePersist(LifecycleEventArgs $args)
{
if (get_class($args->getObject()) == 'Example\Common\ODM\Document\MyDocument') {
$this->loadDependencies($args->getObject(), $args->getObjectManager());
}
}
/**
* Pull relational information from the ORM database to populate
* those fields in the {@link Example\Common\ODM\Document\MyDocument} document that
* require it. Each field is populated as a reference, so it will be
* loaded from the database only if necessary.
*
* @param Example\Common\ODM\Document\MyDocument $document The MyDocument to load dependencies for.
* @param Doctrine\ODM\PHPCR\DocumentManager $documentManager The DocumentManager for the MyDocument.
*/
private function loadDependencies(MyDocument $document, DocumentManager $documentManager)
{
$reflectionClass = $documentManager->getClassMetadata(get_class($document))->getReflectionClass();
$exampleProperty = $reflectionClass->getProperty('example');
$exampleProperty->setAccessible(true);
$exampleProperty->setValue(
$document,
$this->managerRegistry->getManager()->getReference('Example\Common\ORM\Entity\MyEntity', $document->getExampleId())
);
}
}
Everything above works perfectly fine when working with MyDocument
objects. (This is basically an exact implementation of what is described in the Doctrine documentation for blending ORM and MongoDB ODM).
Now the problem is when I also want to do the reverse within the same application -- that is to say, I also want to have an ORM entity which has a listener that fills in a reference or references to ODM documents.
Without adding more code, let's say I extend my services.yml
configuration to:
example.event_listener.my_document:
class: Example\Common\EventListener\MyDocumentEventListener
arguments: [@doctrine]
tags:
- { name: doctrine_phpcr.event_listener, event: postLoad }
- { name: doctrine_phpcr.event_listener, event: prePersist }
example.event_listener.my_entity:
class: Example\Common\EventListener\MyEntityEventListener
arguments: [@doctrine_phpcr]
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: postLoad }
This will now fail, because we have a circular dependency: the container tries to inject the ODM listener into the DocumentManager
's listeners, which in turn tries to inject the EntityManager
, which in turn tries to inject its own listeners, which each try to inject the DocumentManager
, and so on. (Note that this example uses the Registry
rather than the manager, but the result is the same).
I have tried a few different approaches to resolve this but haven't hit on one that works yet. Has anybody been able to get bi-directional listeners between ORM and ODM to work like this in a single project?
I have found few examples around this, unfortunately. My workaround thus far would be to create a service to handle the loading/persisting of these objects and then run everything through that, but it seems very hackish compared to using an elegant event-driven system.
Cleaning up this old question with what turned out to be the correct answer as noted in a comment above: injecting the full container. Normally I avoid this, but it turned out to be the only way to solve this particular problem.
In case anybody is looking for an example of an ODM listener that loads ORM-based dependencies, here is a working example of what I arrived at:
Service definition:
The listener:
This allows the document class to store an ID to an ORM model entity, and each time the document is loaded, it fills in the actual reference to the ORM model within the document. This makes it behave as if it knew about the ORM property all along, and works pretty well.
This example uses PHPCR-ODM but would work just fine for MongoDB ODM as well.