我如何在学说中使用 php8 属性而不是注释? [英] How can i use php8 attributes instead of annotations in doctrine?

查看:15
本文介绍了我如何在学说中使用 php8 属性而不是注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想使用的:

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

  • 这篇关于我如何在学说中使用 php8 属性而不是注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆