1

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!

1 Answer 1

1

As explained in doctrine documentation, you can use table inheritance with annotations with single table inheritance or with class table inheritance.

Single 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
{
    // ...
}

Class table inheritance:

<?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.

Sign up to request clarification or add additional context in comments.

6 Comments

Is the last case correspond to the Concrete Table Inheritance Strategy they are talking?
Yes, I'll add this to my answer.
Thanks! Suppose, I have another child of person (Employer for example). What mapping in this case?
@user2200160 in that case, just add employer to the @DiscriminatorMap annotation, and create a class Employer extending Person.
Okay. I find it here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.