I have changed the configuration of my Symfony project to use PHP attributes with Doctrine in my Entities. I was really happy about this and wanted to give it a try.
I have changed my doctrine.yaml
from annotation
to attribute
orm: auto_generate_proxy_classes: true naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware auto_mapping: true mappings: App: is_bundle: false type: attribute dir: '%kernel.project_dir%/src/Entity' prefix: 'AppEntity' alias: App
and use attributes in my entities
#[ORMEntity(UserRepository::class)] class User implements UserInterface { #[ORMId()] #[ORMGeneratedValue()] #[ORMColumn(type: "integer")] private ?int $id; #[ORMColumn(type: "string", length: 180, unique: true)] private ?string $email; #[ORMColumn(type: "json")] private array $roles = []; }
With this configuration my php bin/console do:sc:up -f
works well.
But when I try to generate a new entity with php bin/console make:entity
I get this error:
[ERROR] Only annotation mapping is supported by make:entity, but the AppEntityToto class uses a different format. If you would like this command to generate the properties & getter/setter methods, add your mapping
configuration, and then re-run this command with the –regenerate flag.
It seems like it’s not possible yet to use maker to generate entity with attribute. Does anyone have found a way to fix this issue or do we just have to wait a new release?
For now I’m using:
"doctrine/annotations": "1.13.1" "doctrine/doctrine-bundle": "2.4.2", "doctrine/orm": "2.9.3" "symfony/maker-bundle": "1.31.1",
Advertisement
Answer
Currently, it’s not supported. There is an issue created asking for this, although I’m 120% sure that the developers already know about PHP attributes, and this feature will get done whenever possible.
In the meantime, you can create your entities manually, without using maker (not that hard), or even use something like Rector to convert Annotations to Attributes. There is a built-in rule for that.