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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。