doujing5435 2016-11-28 15:05
浏览 45
已采纳

实体公共字段的学说继承

I'm using Zend Framework 3 and Doctrine ORM for my web project.

I have several modules in my application (User, Stock, Sales) and some entity models on each Module:

  • User module entities: User, Account, etc..
  • Stock module entities: SKU, StockLevel, etc..
  • Sales module entities: Invoice, PaymentMethod, etc..

By default all entities has common fields, things like:

  • creationDateTime: Date/time of creation
  • creationUser: User who create the entity
  • lastChangeDateTime: Date/time of last entity change
  • lastChangeUser: User who last changed the entity

I don't want to put those fields or every entity, but rather create a project base class that will extend all of my entities. I need to have common methods that will work on all entities, ie:

  /**
   * Update the last change fields
   * @param string $user User that is updating 
   */
  public void updateLastChange($user)
  {
      $this->lastChageDataTime = \Datetime();
      $this->lastChangeUser = $user;
  }

As I can see from the documentation, it seens that I need to use the single table inheritance, but I can't figure out exactly how. Questions:

a) By using single table inheritance, will Doctrine create a base table in database for these fields or will join the base and the entity fields for every entity table, or in other words, will I have just the entity table or this inheritance will create a database table for the base fields also ?

b) Where should I put my base entity so that it can be inherited for all entities on different module ?

I would appreciate it someone could put some example/links on how to do it.

  • 写回答

1条回答 默认 最新

  • doujing5726 2016-11-28 16:06
    关注

    For what you want to do single table inheritance is not what you need.

    There are 2 options:

    1) MappedSuperClass (almost straight from the documentation)

    You make a MappedSuperClass (documentation can be found in chapter 6.1: Mapped Superclasses) and you add those common fields in that base class. Then you extend all the classes that need those fields from your base (mapped super) class.

    /** 
     * @MappedSuperclass 
     */
    class MappedSuperclassBase
    {
        /** @Column(type="datetime") */
        protected $creationDateTime;
    
        /** 
         * @ManyToOne(targetEntity="Application\Entity\User") 
         * @JoinColumn(name="created_by", referencedColumnName="id")
         */
        protected $creationUser;
    
        /** @Column(type="datetime") */
        protected $lastChangeDateTime;
    
        /** 
         * @ManyToOne(targetEntity="Application\Entity\User") 
         * @JoinColumn(name="updated_by", referencedColumnName="id")
         */
        protected $lastChangeUser;
    
        // ... more fields and methods
    }
    
    /** 
     * @Entity 
     */
    class EntitySubClass extends MappedSuperclassBase
    {
        /** @Id @Column(type="integer") */
        private $id;
    
        // ... more fields and methods
    }
    

    2) You use a Trait

    You make a trait (or several separate traits for each field/association) that you use inside all the classes which need to have those common fields.

    trait BaseTrait
    {
        /** @Column(type="datetime") */
        protected $creationDateTime;
    
        /** 
         * @ManyToOne(targetEntity="Application\Entity\User") 
         * @JoinColumn(name="created_by", referencedColumnName="id")
         */
        protected $creationUser;
    
        /** @Column(type="datetime") */
        protected $lastChangeDateTime;
    
        /** 
         * @ManyToOne(targetEntity="Application\Entity\User") 
         * @JoinColumn(name="updated_by", referencedColumnName="id")
         */
        protected $lastChangeUser ;
    
        // ... more fields and methods
    }
    
    /** 
     * @Entity 
     */
    class EntitySubClass
    {
        use BaseTrait;
    
        /** @Id @Column(type="integer") */
        private $id;
    
        // ... more fields and methods
    }
    

    Answers to your questions:

    a) In the docs you can read:

    Single Table Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order to distinguish which row represents which type in the hierarchy a so-called discriminator column is used.

    It would mean that all those entities would share a common table, this absolutely not what you want. It will probably become a huge table (a row for each entity) slowing down your queries. On top of this there will also be columns in the table for all not commonly shared fields and those columns would be empty (null) for the entities that don't have those fields. It also means those not shared fields cannot have a null constraint. Again straight from the documentation:

    For Single-Table-Inheritance to work in scenarios where you are using either a legacy database schema or a self-written database schema you have to make sure that all columns that are not in the root entity but in any of the different sub-entities has to allows null values. Columns that have NOT NULL constraints have to be on the root entity of the single-table inheritance hierarchy.

    Such inheritance is only necessary for entities that are similar to a huge extend and is not suitable for the examples you are talking about in your question.

    b) You can just add the base entity (so the MappedSuperClass) in a common model (like your Application folder).

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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算