drg14799 2014-04-08 16:17
浏览 60
已采纳

Symfony2 + Doctrine。 实体存在于两个包中

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?

  • 写回答

1条回答 默认 最新

  • drsw9390405 2014-04-08 18:24
    关注

    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!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug