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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)