質問

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);
役に立ちましたか?

解決

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
{
    // ...
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top