How to declare doctrine2 inheritance the yaml way?
I don\'t find any code snippets, examples or cookbook articles about this in the doctrine documentation.
W
It exist three main strategies to manage inheritance in relational database.
You can find how create each of this strategies on the Symfony website in the YAML format with Doctrine 2 : YAML Inheritance with Doctrine
Try doing a simple model inheritance using the examples in the documentation (which are in @Annotations format), and converting them to yaml using the doctrine command line tool with parameters orm:convert-mapping (which converts mapping information between supported formats). More information here.
There are a few different types of inheritance in Doctrine2. Here are examples for the two most common types:
# MyProject.Model.Person.dcm.yml
MyProject\Model\Person:
type: mappedSuperClass
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 50
...
# MyProject.Model.EmployedPerson.dcm.yml
MyProject\Model\EmployedPerson:
type: entity
fields:
occupation:
type: string
length: 100
...
Then, in your PHP classes:
# Person.php
<?php
namespace MyProject\Model;
class Person
{
private $id;
private $name;
// Add public getters and setters
}
# EmployedPerson.php
<?php
namespace MyProject\Model;
class EmployedPerson extends Person
{
private $occupation;
// Add public getters and setters
}
There are two main things you need to do to make this work: use type: mappedSuperClass
instead of type: entity
on the parent, and to ensure that your PHP child class extends the parent class.
You can add whatever fields and relationships you need to either class, although you should note the warning in the docs regarding the relationships you can add to the parent:
A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all. Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. For further support of inheritance, the single or joined table inheritance features have to be used.
Conveniently, the docs already give an example of a YAML config for Single-Table Inheritance:
MyProject\Model\Person:
type: entity
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: discr
type: string
discriminatorMap:
person: Person
employee: Employee
MyProject\Model\Employee:
type: entity