Вопрос

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