How to change and entity type in Doctrine2 CTI Inheritance

前端 未结 4 980
走了就别回头了
走了就别回头了 2020-12-17 18:38

How (if possible at all) do you change the entity type with Doctrine2, using it\'s Class Table Inheritance?

Let\'s say I have a Person parent class type

4条回答
  •  粉色の甜心
    2020-12-17 18:50

    In Doctrine2, when you have your parent entity class, Person set as:

    /**
     * @Entity
     * @InheritanceType("JOINED")
     * @DiscriminatorColumn(name="discr", type="string")
     * @DiscriminatorMap({"person" = "Person", "employee" = "Employee", , "client" = "Client"})
     */
    class Person
    {
        // ...
    }
    

    and sub classes such as Client set as:

    /** @Entity */
    class Client extends Person
    {
        // ...
    }
    

    when you instantiate Person as:

    $person = new Person();
    

    Doctrine2 checks your @DiscriminatorMap statement (above) for a corresponding mapping to Person and when found, creates a string value in the table column set in @DiscriminatorColumn above.

    So when you decide to have an instance of Client as:

    $client = new Client();
    

    Following these principles, Doctrine2 will create an instance for you as long as you have declared the parameters in the @DiscriminatorMap. Also an entry will be made on the Person table, in the discr column to reflect that type of entity class that has just been instantiated.

    Hope that helps. It's all in the documentation though

提交回复
热议问题