Using Relationships with Multiple Entity Managers

Using different object managers (entity managers) doesn’t allow the object graphs to intersect. That case is too complex and isn’t managed by Doctrine ORM.

If you need such a case, keep the object graphs disconnected by saving the identifiers of the related objects (old style) instead of a reference to them, then manually get the objects through services. You can find a fairly good example of how this would work in an example of connection between Doctrine2 ORM and Doctrine2 MongoDB ODM. Alternatively, you could also use a @PostLoad event listener that populates data in your entities by creating the link through the repositories I’ve linked in the example. Same for @PostPersist (which should instead extract the identifiers for the related objects), but beware that this technique can become really messy.

Also, if your RDBMS supports cross-database operations on a single host, you can just use a single EntityManager and reference the other table with @ORM\Table(name="schemaname.tablename").

Leave a Comment