问题
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