Reputation: 104
I would like to implement table inheritance in Symfony2. It is possible here with symfony1.4.
How can I use it with symfony2 with annotations schema.?
Thanks for taking your time to look at this!
Upvotes: 1
Views: 100
Reputation: 6758
As explained in doctrine documentation, you can use table inheritance with annotations with single table inheritance or with class table inheritance.
<?php
namespace MyProject\Model;
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
/**
* @Entity
*/
class Employee extends Person
{
// ...
}
<?php
namespace MyProject\Model;
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
/** @Entity */
class Employee extends Person
{
// ...
}
In the first case, all records will be stored in the same table, with a field to discriminate the different types of them.
In the second case, a "parent" table will contains all records with commons fields between the classes, and the other fields depending on the classes will be stored in different tables for the extending classes.
Up to you to chose which one to use, depending of your needs.
In the first case, having a lot of properties in your extending class will result in a lot of empty fields in the table, whereas in the second case, you'll have to merge two tables (the "parent" one and the extended one) to get all the fields of a record.
To compare it with your linked Symfony1.4 documentation, single table inheritance is equivalent to the "Column Aggregation Table Inheritance Strategy" of SF1.4, whereas class table inheritance is equivalent to "Concrete Table Inheritance Strategy" of SF1.4.
Upvotes: 1