在 Symfony2 中使用同一包中的两个实体管理器
我正在尝试与同一个捆绑包的两个实体管理器合作。我的配置是这样的:
orm:
default_entity_manager: default
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
有什么方法可以告诉哪些实体属于哪个实体管理器?如果我想使用不属于默认实体管理器的表,它现在会崩溃。
- 这里更新
是我的连接配置:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
electra:
dbname: electra
user: root
password: 123123
host: 2.2.2.2
port: 2
orm:
default_entity_manager: electra
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
I'm trying to work with two entity managers for the same bundle. My configuration is like this:
orm:
default_entity_manager: default
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
Is there any way to tell which entities belong to which entity manager? It crashes now if I want to work with a table which doesn't belong to the default entity manager.
- UPDATE
here is my configuration for the connection:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
electra:
dbname: electra
user: root
password: 123123
host: 2.2.2.2
port: 2
orm:
default_entity_manager: electra
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在同一包中使用多个实体管理器,您必须为每个实体管理器配置映射选项。
http://symfony.com/doc/current/reference/configuration/doctrine.html
示例 您现在可以使用控制台
通过 --em 参数管理
数据库 例如:更新商店实体管理器的数据库
从 Your\Bundle\Entity\SecondDb 读取映射信息
例如:更新默认实体管理器的数据库
从以下位置读取映射信息你的\Bundle\Entity\FirstDb
For using multiple entitymanager in same bundle you have to config mapping options for each entitymanager.
http://symfony.com/doc/current/reference/configuration/doctrine.html
Exemple off config file
You can now use console for managing your db with the --em parameter
Ex : update database for shop entitymanager
Read mapping information from Your\Bundle\Entity\SecondDb
Ex : update database for default entitymanager
Read mapping information from Your\Bundle\Entity\FirstDb
好的。尝试编辑您的原始帖子,但正在等待同行评审。不确定这需要多长时间。尝试将您的配置更改为:
现在完全清除您的缓存,以确保然后运行:
您应该得到相同的结果。我在我的系统上测试了这个,所以我相当确定如果你不这样做,那么你在某个地方有一些拼写错误。
所以地图信息正在发挥作用。下一步是验证两个数据库是否与您的实体架构匹配。所以这样做:
两者都不应该产生任何输出。如果出现这种情况,则意味着您的数据库与您的实体不匹配,需要在查询起作用之前解决该问题(可能使用 --force 选项)。
一旦数据库同步,那么您可能应该使用doctrine:query:dql并对两个管理器进行测试查询。然后回到你的代码。
===========================================
现在已经明白真正的目标是让两个实体管理器指向同一组实体,但以某种方式指示每个实体管理器应该将自己限制为这些实体的某个集合。 S2 并不支持这种功能。
您可以查看 Doctrine 手册,看看它如何处理实体元数据,也许可以用它做一些事情,但这可能会变得复杂。
S2 真正提供的唯一一件事是能够使用映射属性将实体管理器绑定到一个或多个捆绑包中的所有实体。如果您想与另一个捆绑包共享一个捆绑包中的七个实体中的三个,那么您只需在第二个捆绑包中重新创建这些实体即可。可能通过扩展类来避免代码重复。
我想你可能想稍微改变一下你的方法。如果您有一组与多个捆绑包共享的核心实体,请将它们放入自己的捆绑包中。每个后续捆绑包都可以添加其他实体。
Ok. Tried to edit your original post but it's waiting for peer review. Not sure how long that takes. Try changing your config to:
Now completely blow away your cache just to be sure then run:
You should get identical results. I tested this on my system so I'm fairly certain that if you don't then you have some typo somewhere.
So mapping info is working. Next step is to verify that both databases match your entity schema. So do this:
Neither should produce any output. If one does then it means your database does not match your entities and that needs to be resolved (possibly using the --force option) before queries will work.
Once the databases are in sync then you should probably use doctrine:query:dql and do a test query against both managers. Then go back into your code.
=========================================
It has now been understood that the real goal is to have two entity managers point to the same set of entities but somehow indicate that each entity manager should limit itself to a certain set of those entities. And that is not something the S2 supports out of the box.
You could look through the Doctrine manual and see how it handles the entity metadata and maybe do something with that but that could get complicated.
The only thing that S2 really offers is the ability to bind an entity manager to all the entities in one or more bundles using the mapping attribute. If you wanted to share say three of seven entities from one bundle with another bundle then you would simply recreate those entities in the second bundle. Possibly by extending the class so as to avoid code duplication.
I think you might want to alter your approach a bit. If you have a set of core entities shared with multiple bundles then put those in their own bundle. Each follow on bundle can then add additional entities.