Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Relationships with Multiple Entity Managers

I am wondering if it is possible to create a relationship between two entities that reside in separate databases.

For example if we took the solution found here http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html and created a one to many relationship with Users in the customer database to Posts in the default database.

Is this something that is supported by Symfony2 and Doctrine?

like image 462
Mike Avatar asked Jul 13 '12 02:07

Mike


1 Answers

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").

like image 155
Ocramius Avatar answered Sep 21 '22 12:09

Ocramius