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?

图片转代码服务由CSDN问答提供 功能建议

前段时间我将常用功能移到单独的包中,因此可以在其他项目中重复使用它们。 在其中一个包中,我有一个名为Person的实体。 Person与由其他人控制的现有DB表相关。 在我的可重用捆绑包中,Person实体映射了人员数据库表中的一些字段,而不是所有字段。

在我当前的项目中,我需要在人员数据库表中使用更多字段, 但是我不能改变可重用包中的实体,因为现在它已经在各种项目中使用了。

我试图扩展现有的Person实体,但app / console doctrine:schema:create 忽略我的子类中的映射字段。 如果我将@ORM表映射添加到子类,app / console doctrine:schema:create complains并说“person表已经存在”(或类似的东西)。

是 有没有办法在我的新包中扩展现有的Person实体,并为其添加更多字段?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题