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).

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100