camelot / doctrine-inheritance-mapping
Doctrine inheritance mapping library
Requires
- php: ^7.4
- doctrine/annotations: ^1.7
- doctrine/orm: ^2.7
Requires (Dev)
- camelot/coding-style: ^2.0
- doctrine/doctrine-bundle: ^2.0
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8.4
- ramsey/uuid-doctrine: ^1.5
- symfony/config: ^5.0
- symfony/debug-bundle: ^5.0
- symfony/framework-bundle: ^5.0
- symfony/http-kernel: ^5.0
- symfony/phpunit-bridge: ^5.0
- symfony/var-dumper: ^5.0
- symfony/yaml: ^5.0
README
NOTE: For legacy PHP support (7.1+) please use the 1.0 branch.
Installation
Open a command console, enter your project directory and execute:
$ composer require camelot/doctrine-inheritance-mapping
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Standalone Configuration
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorMapLoader; use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\DocParser; use Doctrine\ORM\Configuration; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\Driver\AnnotationDriver; // Annotation reader & driver $reader = new AnnotationReader(new DocParser()); $driver = new AnnotationDriver($reader); $driver->addPaths(['/path/to/entities']); // Doctrine configuration $config = new Configuration(); $config->setMetadataDriverImpl($driver); $classMetadata = new ClassMetadata(YourEntityName::class); $loader = new DiscriminatorMapLoader($reader, $config); $loader->loadClassMetadata($classMetadata);
Framework Configuration
Symfony Bundle
If using the Symfony Framework, you can enable the bundle by adding it to the
list of registered bundles in the config/bundles.php
file of your project:
// config/bundles.php return [ // ... Camelot\DoctrineInheritanceMapping\Bridge\Symfony\DoctrineInheritanceMappingBundle::class => ['all' => true], ];
Usage
Single Table Inheritance
@DiscriminatorMapItem
Annotation
Doctrine's Single Table Inheritance is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table.
The mapping is handled by a "discriminator" column, defined in the mapping definition of the parent class. This column value defines the entity class to use, based on the inheritance hierarchy. This binds the parent to the children and mixes responsibilities in the process.
To separate these concerns, this library provides the @DiscriminatorMapItem
annotation for use in each entity in a hierarchy, replacing the parent class
use of Doctrine's @DiscriminatorMap
, thus eliminating the need to update the
parent for each subclass.
Example
Parent Class
<?php namespace App\Entity; use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorMapItem; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() * @ORM\InheritanceType("SINGLE_TABLE") * @DiscriminatorMapItem(value="SingleTable") */ class SingleTable { // ... }
NOTE: Using @DiscriminatorColumn
along with @DiscriminatorMapItem
is
optional, and has been omitted above for clarity.
Child(ren) Class
<?php namespace App\Entity; use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorMapItem; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() * @DiscriminatorMapItem(value="SingleTableChild") */ class SingleTableChild extends SingleTable { // ... }