Recently we upgraded our applications to PHP8.
Since PHP8 introduced attributes and doctrine/orm supports them as of version 2.9 it seemed like a good idea to utilize this feature to incrementally (ie. not all entities at once) update entity metadata to the attributes' format.
In order to do so I need to somehow register both Doctrine\ORM\Mapping\Driver\AnnotationDriver and Doctrine\ORM\Mapping\Driver\AttributeDriver to parse the metadata.
The tricky part is to register both parsers for a set of entities decorated either using annotations or attributes. From the point of Doctrine\ORM\Configuration it seems what I need is not possible.
Am I correct (in assumption this cannot be reasonably achieved) or could this be done in some not-very-hackish way?
Doctrine by itself doesn't offer this possibility. But we can implement a custom mapping driver to make this happen.
The actual implementation could look like this:
In a nutshell:
AttributeDriverfirst, then fallbacks to theAnnotationDriverin case the class under inspection is not evaluated as a valid entityDoctrine\Persistence\Mapping\Driver\MappingDriverinterface after extendingDoctrine\Persistence\Mapping\Driver\AnnotationDriverclass only 2 methods have to be implementedMappingExceptions by parsing the message is not elegant at all, but there is no better attribute to distinguish by; having different exception subtypes or some unique code per mapping error case would help a lot to differentiate between individual causes of mapping errorsThe
HybridMappingDrivercan be hooked up in anEntityManagerFactorylike this: