11

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 ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Thibaut
  • 111
  • 1
  • 3
  • http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html – Cerad Aug 13 '13 at 12:24
  • 3
    This link shows how to make dynamic relations between entities in different bundle. I dont need to link entities from different bundle. I want to have same entity name and table in different bundles. – Thibaut Aug 14 '13 at 08:22
  • Hello, The obvious solution to your problem is to make a different entity (with different table name). You can extends from the same model. That's obvious because you say you can't do an inheritance, so in term of db, you also cannot. (because of you're using an ORM) Without your logic, I can't give you a more precise help. – Nek Nov 03 '14 at 12:43
  • 2
    I don't get it: If it's __the same entity__ and __the same table__, why not have it in the main bundle and use it the other bundles. I don't see the problem with a one-way dependency between the bundles. – Jasper N. Brouwer Nov 04 '14 at 09:58
  • 1
    I have the same problem. I have multiple entity that point to the same table. I use it to centralize my uploads. It works, but i can't remove item because of foreign keys – Chopchop Dec 30 '15 at 08:03

1 Answers1

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

Community
  • 1
  • 1
Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35
  • Can you please explain how to use the resolved entities as objects (ie. in a controller, as if I had just a regular entity in the bundle)? – wiktus239 Oct 02 '15 at 11:54