Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine inheritance for entities common fields

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.

like image 272
Mendes Avatar asked Dec 23 '22 23:12

Mendes


1 Answers

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

like image 78
Wilt Avatar answered Dec 26 '22 16:12

Wilt