Skip to content

Commit 871defa

Browse files
README
1 parent be062ea commit 871defa

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Camelot Doctrine Inheritance Mapping
2+
====================================
3+
4+
Installation
5+
------------
6+
7+
Open a command console, enter your project directory and execute:
8+
9+
```console
10+
$ composer require camelot/doctrine-inheritance-mapping
11+
```
12+
13+
This command requires you to have Composer installed globally, as explained
14+
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
15+
of the Composer documentation.
16+
17+
### Standalone Configuration
18+
19+
```php
20+
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorMapLoader;
21+
use Doctrine\Common\Annotations\AnnotationReader;
22+
use Doctrine\Common\Annotations\DocParser;
23+
use Doctrine\ORM\Configuration;
24+
use Doctrine\ORM\Mapping\ClassMetadata;
25+
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
26+
27+
// Annotation reader & driver
28+
$reader = new AnnotationReader(new DocParser());
29+
$driver = new AnnotationDriver($reader);
30+
$driver->addPaths(['/path/to/entities']);
31+
32+
// Doctrine configuration
33+
$config = new Configuration();
34+
$config->setMetadataDriverImpl($driver);
35+
36+
$classMetadata = new ClassMetadata(YourEntityName::class);
37+
38+
$loader = new DiscriminatorMapLoader($reader, $config);
39+
$loader->loadClassMetadata($classMetadata);
40+
```
41+
42+
### Framework Configuration
43+
44+
#### Symfony Bundle
45+
46+
If using the Symfony Framework, you can enable the bundle by adding it to the
47+
list of registered bundles in the `config/bundles.php` file of your project:
48+
49+
```php
50+
// config/bundles.php
51+
52+
return [
53+
// ...
54+
Camelot\DoctrineInheritanceMapping\Bridge\Symfony\DoctrineInheritanceMappingBundle::class => ['all' => true],
55+
];
56+
```
57+
58+
Usage
59+
-----
60+
61+
### Single Table Inheritance
62+
63+
#### `@DiscriminatorMapItem` Annotation
64+
65+
Doctrine's [Single Table Inheritance][single-table-inheritance] is an
66+
inheritance mapping strategy where all classes of a hierarchy are mapped to a
67+
single database table.
68+
69+
The mapping is handled by a "discriminator" column, defined in the mapping
70+
definition of the parent class. This column value defines the entity class to
71+
use, based on the inheritance hierarchy. This binds the parent to the children
72+
and mixes responsibilities in the process.
73+
74+
To separate these concerns, this library provides the `@DiscriminatorMapItem`
75+
annotation for use in *each* entity in a hierarchy, replacing the parent class
76+
use of Doctrine's `@DiscriminatorMap`, thus eliminating the need to update the
77+
parent for each subclass.
78+
79+
##### Example
80+
81+
###### Parent Class
82+
83+
```php
84+
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorEntry;
85+
use Doctrine\ORM\Mapping as ORM;
86+
87+
/**
88+
* @ORM\Entity()
89+
* @ORM\InheritanceType("SINGLE_TABLE")
90+
* @DiscriminatorMapItem(value="SingleTable")
91+
*/
92+
class SingleTable
93+
{
94+
// ...
95+
}
96+
97+
```
98+
99+
**NOTE:** Using `@DiscriminatorColumn` along with `@DiscriminatorMapItem` is
100+
optional, and has been omitted above for clarity.
101+
102+
###### Child(ren) Class
103+
104+
```php
105+
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorEntry;
106+
use Doctrine\ORM\Mapping as ORM;
107+
108+
/**
109+
* @ORM\Entity()
110+
* @DiscriminatorMapItem(value="SingleTableChild")
111+
*/
112+
class SingleTableChild extends SingleTable
113+
{
114+
// ...
115+
}
116+
```
117+
[single-table-inheritance]: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#single-table-inheritance

0 commit comments

Comments
 (0)