doctrine persist php class which inherit doctrine entity

余生长醉 提交于 2019-12-24 06:45:07

问题


Is it possible to have a php class which extends a doctrine entity and to persist it ?

example:

/**
 * @Entity
 */
class A {
  /**
   * @ORM\ManyToMany(targetEntity="C", mappedBy="parents")
   */
  protected $children;
}

class B extends A {
...
}

class C {
  /**
   * @ORM\ManyToMany(targetEntity="A", inversedBy="children")
   */
  protected $parents;
}

$b = new B();
$em->persist($b);

回答1:


Yes it's possible, this is called inheritance mapping, but the child class has to be explicitly declared as @Entity and its mapping has to be explicitly defined as well (if the child class adds extra properties).

The most common form of inheritance mapping is single table inheritance, here is an example of such mapping from the Doctrine manual:

namespace MyProject\Model;

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

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


来源:https://stackoverflow.com/questions/9501755/doctrine-persist-php-class-which-inherit-doctrine-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!