Doctrine YAML annotations
, (*1)
One of the great features of Doctrine 2 is extensibility.
Doctrine offers multiple ways to specify mapping information,
but the most of the extensions only supports Annotations configuration., (*2)
This package adds custom annotations to your YAML mapping files., (*3)
What is currently supported:
- property annotations (fields and embeddables)
- class annotations, (*4)
Installation
The best way to install fmasa/doctrine-yaml-annotations is using Composer:, (*5)
$ composer require fmasa/doctrine-yaml-annotations
For example let's configure the Consistence extension for Doctrine., (*6)
First we have to create annotation reader:, (*7)
use Fmasa\DoctrineYamlAnnotations\YamlReader;
$configuration = $entityManager->getConfiguration();
$reader = new YamlReader($configuration, [
'enum' => EnumAnnotation::class
]);
Second argument for AnnotationReader is optional map with entity aliases., (*8)
Add annotations to your mapping files:, (*9)
Some\Entity:
...
fields:
state:
type: enum_string
annotations:
Consistence\Doctrine\Enum\EnumAnnotation: # or just enum
class: StateEnum
Now you can read annotations just using Doctrine\Common\Annotations\Reader API:, (*10)
$reader->getPropertyAnnotation(
(new \ReflectionClass(Some\Entity::class))->getProperty('state'),
EnumAnnotation::class
); // returns instance of EnumAnnotation { class => "StateEnum" }