PHPackages                             camelot/doctrine-inheritance-mapping - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. camelot/doctrine-inheritance-mapping

ActiveLibrary[Database &amp; ORM](/categories/database)

camelot/doctrine-inheritance-mapping
====================================

Doctrine inheritance mapping library

v2.0.2(6y ago)117MITPHPPHP ^7.4

Since Oct 6Pushed 6y ago1 watchersCompare

[ Source](https://github.com/CamelotProject/doctrine-inheritance-mapping)[ Packagist](https://packagist.org/packages/camelot/doctrine-inheritance-mapping)[ RSS](/packages/camelot-doctrine-inheritance-mapping/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (2)Dependencies (14)Versions (8)Used By (0)

Camelot Doctrine Inheritance Mapping
====================================

[](#camelot-doctrine-inheritance-mapping)

**NOTE:** For legacy PHP support (7.1+) please use the 1.0 branch.

Installation
------------

[](#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](https://getcomposer.org/doc/00-intro.md)of the Composer documentation.

### Standalone Configuration

[](#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

[](#framework-configuration)

#### Symfony Bundle

[](#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
-----

[](#usage)

### Single Table Inheritance

[](#single-table-inheritance)

#### `@DiscriminatorMapItem` Annotation

[](#discriminatormapitem-annotation)

Doctrine's [Single Table Inheritance](https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#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

[](#example)

###### Parent Class

[](#parent-class)

```
