Ray H
Ray H

Reputation: 333

Doctrine2: Should parent classes in single table inheritance be abstract?

I'm writing constructors for my classes in a Doctrine2 application, let's say Fruits, Apple, Bananas.

Fruits is the parent class, where Apples and Bananas inherit from Fruits using single table inheritance on field type.

On the Doctrine2 documentation page, there is an example provided for single table inheritance. If we are always discriminating using Single Table Inheritance, should the base class Fruits be abstract because the discriminator field must always be set? If so, should the constructor for Fruits also be protected to prevent this behavior?

Upvotes: 1

Views: 1712

Answers (1)

Lee Davis
Lee Davis

Reputation: 4756

As there are no methods in your parent class "Fruits" that you need to redeclare I don't think there is an explicit need for it to be declared as abstract.

Also you may find a use case where you may want an instance of "Fruit" to be persisted (undetermined as to what type of fruit it is). Marking the parent as abstract will prevent you from being able to do this.

Maybe fruit is a bad example. But the Person example they have in the documentation is better. Employees will inherit Person definitions. But I may also want to persist just an instance of Person, undetermined of type. Hence the "person" = "Person" in the @DiscriminatorMap.

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html#single-table-inheritance

Upvotes: 3

Related Questions