如何在学说中使用php8属性而不是注释?
这是我想使用的:
#[ORMColumn(type="string")]
而不是这个:
/**
* @ORMColumn(type="string")
*/
但我收到此错误:
(error: Class 'Column' is not annotated with 'Attribute' )
是因为 Doctrine 还不支持它,还是我错过了什么?
回答
正如@Seb33300 所说,是的,现在可以在 Doctrine ORM 2.9 中实现。但是对于 Symfony,您需要做的还不止这些。以下是升级步骤的完整列表:
-
升级 Doctrine ORM:
"doctrine/orm": "^2.9". -
升级教义包:
"doctrine/doctrine-bundle": "^2.4". -
设置
doctrine.orm.mappings.App.type: attribute(默认设置为annotation):# config/packages/doctrine.yaml doctrine: orm: mappings: App: type: attribute -
对您的实体应用类似的更改:
--- Dummy.php.old Mon Jun 07 00:00:00 2021 +++ Dummy.php Mon Jun 07 00:00:00 2021 @@ -7,15 +7,11 @@ use AppRepositoryDummyRepository; use DoctrineORMMapping as ORM; -/** - * @ORMEntity(repositoryClass = DummyRepository::class) - */ +#[ORMEntity(repositoryClass: DummyRepository::class)] class Dummy { - /** - * @ORMId - * @ORMGeneratedValue - * @ORMColumn(type = 'integer') - */ + #[ORMId] + #[ORMGeneratedValue] + #[ORMColumn(type: 'integer')] private $id; }
- The change from "annotation" to "attribute" is critical. Thank you for documenting it.
回答
编辑:Doctrine 2.9现已发布,支持 PHP 8 属性!
PHP 8 注释已合并到2.9.x尚未发布的Doctrine ORM分支中:https :
//github.com/doctrine/orm/pull/8266
以下是与此功能相关的文档参考:https :
//www.doctrine-project.org/projects/doctrine-orm/en/current/reference/attributes-reference.html