This is an example of using Doctrine ORM entity inheritance with the Symfony Flex framework. It is mostly a reminder to myself, but others may find it useful as a reference.
More indepth information about Doctrine ORM inheritance:
This package contains six Doctrine ORM entity classes in src/Entity that are configured via
annotations. The included entities use inheritance with two methods (SINGLE_TABLE
and JOINED
).
There are two separate data structures:
-
Animal
- id
- color
- Cat
- name
- Dog
- kennel
- Cat
-
Vehicle
- id
- color
- Moped
- tuning
- Truck
- wheelage
- Moped
The relationships are configured using annotations in the parent class, for example:
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"animal" = "Animal", "cat" = "Cat", "dog" = "Dog"})
The Animal
base entity class is inherited using SINGLE_TABLE
strategy by the Cat
and Dog
sub entities. The generated schema when using this strategy contains a single table with all
the columns for each sub entity.
id | discr | color | name | kennel |
---|---|---|---|---|
1 | cat | black | Jallu | |
2 | cat | grey | Ossi | |
3 | dog | brown | The Dogg Pound |
The Vehicle
base entity class is inherited using JOINED
strategy by Truck
and Moped
classes. With this strategy generated schema contains individual tables for each sub entity.
id | color | discr |
---|---|---|
1 | Blue | truck |
2 | Red | truck |
3 | Black | moped |
id | wheelage |
---|---|
1 | 10 |
2 | 18 |
id | tuning |
---|---|
3 | Regular |
Prerequisites for installation are PHP 7.1, Composer and a supported database (Postgres, MariaDB, MySQL, SQLite...).
The first step is to copy the .env.dist
to .env
and configure a working database connection.
Once this is done, proceed with installing packages and setting up the database:
$ composer install
$ ./bin/console doctrine:database:create
$ ./bin/console doctrine:schema:update --force
Once the the installation is complete, proceed with running the local devserver:
$ make serve
You can find the entity administration interface (using EasyAdminBundle) in http://localhost:8000/admin
Create some entities and observe the database schema and entities.