steveYeah
steveYeah

Reputation: 351

Symfony2 + Doctrine. Entity exists in two bundles

A while back I moved common functionality into separate bundles, so they can be reused in other projects. In one of these bundles I have an Entity named Person. Person is related to an existing DB table that is controlled by someone else. In my reusable bundle the Person entity maps some of the fields from the person DB table, not all of them.

In my current project I need to use more fields in the person DB table, but I cannot alter the entity in the reusable bundle as this is now used in various projects.

I have tried to extend the existing Person entity, but the app/console doctrine:schema:create ignores the mapped fields in my child class. If I add the @ORM table mapping to the child class, the app/console doctrine:schema:create complains and says "The person table already exists" (or something similar).

Is there a way to extend the existing Person entity in my new bundle, and add more fields to it?

Upvotes: 0

Views: 438

Answers (1)

xiidea
xiidea

Reputation: 3394

As per Symfony2 Documentation you can't just override an entity of a bundle.

Due to the way Doctrine works, it is not possible to override entity mapping of a bundle. However, if a bundle provides a mapped superclass (such as the User entity in the FOSUserBundle) one can override attributes and associations. Learn more about this feature and its limitations in the Doctrine documentation.

While creating a bundle with the hope to use it in several, it is good practice to Implement entities like the FOSUserBundle. Or to define Entity class as an Abstract base class, so you can just create a child bundle and override any part you like easily. As entity override are not possible, you have to just implement a concrete entity class by extending the abstract class defined in your shared bundle.

Hope you'v got your answer.

Happy coding!!

Upvotes: 1

Related Questions