Doctrine persist php class that inherits doctrine

Is it possible to have a php class that extends the doctrine object and saves 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
source share
1 answer

Yes, it’s possible, it’s called mapping inheritance , but the child class must be explicitly declared as @Entity , and its mapping must also be explicitly defined (if the child class adds additional properties).

The most common form of inheritance mapping is unidirectional table inheritance, here is an example of such a 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 { // ... } 
+3
source

Source: https://habr.com/ru/post/1400368/


All Articles