我如何在学说中使用 php8 属性而不是注释?

2022-01-16 00:00:00 php symfony doctrine php-8 doctrine-orm

这是我想使用的:

#[ORMColumn(type: "string")]

而不是这个:

/**
 *  @ORMColumn(type="string")
 */

但是我收到了这个错误:

But I'm getting this error:

(error: Class 'Column' is not annotated with 'Attribute' )

是因为 Doctrine 还不支持,还是我遗漏了什么?

Is it because Doctrine does not support it yet, or am I missing something?

推荐答案

正如@Seb33300 所说,是的,它是 现在可能.但是对于 Symfony,您需要做的还不止这些.以下是升级步骤的完整列表:

As @Seb33300 says, yes, it's now possible in Doctrine ORM 2.9. But for Symfony you need to do a little bit more than that. Here is a full list of steps to upgrade:

  1. 升级 Doctrine ORM:doctrine/orm":^2.9".

升级 Doctrine Bundle:doctrine/doctrine-bundle":^2.4".

Upgrade Doctrine Bundle: "doctrine/doctrine-bundle": "^2.4".

设置doctrine.orm.mappings.App.type: attribute(默认设置为annotation):

# config/packages/doctrine.yaml

doctrine:
  orm:
    mappings:
      App:
        type: attribute

  • 对您的实体应用类似的更改:

  • Apply similar changes to your entities:

    --- 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;
     }
    

  • 相关文章