使用与多个实体管理器的关系 [英] Using Relationships with Multiple Entity Managers

查看:70
本文介绍了使用与多个实体管理器的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在驻留在单独的数据库中的两个实体之间创建关系。



例如,如果我们在这里找到解决方案 http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html ,并创建了与客户数据库中的用户的一对多关系到默认数据库中的帖子。 p>

这是Symfony2和Doctrine支持的东西?

解决方案

使用不同的对象管理器(实体管理器)不允许对象图相交。这种情况太复杂了,不由Doctrine ORM管理。



如果需要这种情况,通过保存相关对象的标识符来保持对象图断开旧样式),而不是引用它们,然后通过服务手动获取对象。您可以在 Doctrine2 ORM和Doctrine2 MongoDB ODM之间的连接示例。或者,您还可以使用 @PostLoad 事件监听器,通过在示例中链接的存储库创建链接来填充实体中的数据。对于 @PostPersist (它应该提取相关对象的标识符)相同,但请注意,这种技术可能变得非常混乱。



另外,如果您的RDBMS支持在单个主机上进行跨数据库操作,则可以使用单个EntityManager,并引用另一个表,使用 @ ORM\Table(name =schemaname)。 tablename)


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?

解决方案

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

这篇关于使用与多个实体管理器的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆