Implementing single table inheritance
Relational databases do not support inheritance. If we need to store inheritance in the database, we should somehow support it through code. This code should be efficient, so that it should generate as few JOINs as possible. A common solution to this problem was described by Martin Fowler and is named single table inheritance.
When we use this pattern, we store all the class tree data in a single table and use the type field to determine a model for each row.
As an example, we will implement the single table inheritance for the following class tree:
Car
|- SportCar
|- FamilyCar
Getting ready
- Create a new application using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html ...
Get Yii2 Application Development Cookbook - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.