doctrine persist php class继承了doctrine实体

时间:2012-02-29 15:19:38

标签: doctrine doctrine-orm persist

是否有可能拥有一个扩展一个教义实体并坚持它的php类?

示例:

/**
 * @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 个答案:

答案 0 :(得分:3)

是的,这可能是inheritance mapping,但子类必须显式声明为@Entity,并且必须明确定义其映射(如果子类添加了额外的属性)

最常见的继承映射形式是单表继承,以下是Doctrine手册中此类映射的示例:

namespace MyProject\Model;

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

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