Doctrine2 / Symfony2 - Multiple entities on same table

南楼画角 提交于 2019-12-23 07:38:29

问题


In a Symfony2 application I have a MainBundle and distinct bundles which can be enabled or not. In the MainBundle I need to have the Model and a basic Entity. In an OtherBundle an Entity with the same table name than Entity in MainBundle.

Fixtures in MainBundle need to be loaded with or without the other bundles than MainBundle :

MainBundle
- Model 
- Entity (Table name "test")
- Fixtures 

OtherBundle
- Entity (Table name "test")
- Fixtures

OtherBundle2
- Entity (Table name="test")
- Fixtures

If i used the @ORM\MappedSuperclass for the Model, a @ORM\Entity for the Entity in MainBundle and @ORM\Entity in OtherBundle then Doctrine2 stop with the error "table already exists".

I cant use the Inheritance table as my model dont need to know about other entities in the other bundles. The @ORM\DiscriminatorMap cant point to OtherBundle.

Is there a way to do this ?


回答1:


As mentioned by Jasper N. Brouwer it's esentially the same entity and the same table, so there is no point in doing what you're trying to do.

Create your entity in a bundle named for example "SharedEntityBundle" and use resolve_target_entity to relate to this entity from other bundles without them knowing about eachother.

http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html

That being said, there seems to be a solution with multiple entity managers: Symfony 2 / Doctrine 2: Two Entities for the same table, use one in favour of the other



来源:https://stackoverflow.com/questions/18206201/doctrine2-symfony2-multiple-entities-on-same-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!