Skip to content

A simple Symfony Flex app example of using Doctrine ORM with inheritance of Entity classes

Notifications You must be signed in to change notification settings

0x6d6c/doctrine-inheritance-example

 
 

Repository files navigation

Doctrine ORM entity inheritance example (with Symfony Flex)

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:

Structure and table overview

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
  • Vehicle

    • id
    • color
      • Moped
        • tuning
      • Truck
        • wheelage

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"})

Single table inheritance

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.

animal table

id discr color name kennel
1 cat black Jallu
2 cat grey Ossi
3 dog brown The Dogg Pound

Joined inheritance

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.

vehicle table

id color discr
1 Blue truck
2 Red truck
3 Black moped

truck table

id wheelage
1 10
2 18

moped table

id tuning
3 Regular

Installation

Prerequisites for installation are PHP 7.1, Composer and a supported database (Postgres, MariaDB, MySQL, SQLite...).

Alternatively, you can use Docker/Docker Compose (see below).

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

Installation and set-up with Docker

If you want to use Docker, run:

$ docker-compose up -d --build

SSH into the running container:

$ docker exec --interactive --tty doctrine_inheritance_example-php bash

Set up the project:

$ composer install
$ ./bin/console doctrine:database:create
$ ./bin/console doctrine:schema:update

You can find the entity administration interface (using EasyAdminBundle) in http://localhost:8333/admin

Create some entities and observe the database schema and entities.

About

A simple Symfony Flex app example of using Doctrine ORM with inheritance of Entity classes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 78.8%
  • Makefile 12.9%
  • Dockerfile 5.5%
  • HTML 2.8%