这是我想要使用的:
#[ORM\Column(type: "string")]
使用以下内容替换:
/**
* @ORM\Column(type="string")
*/
但我遇到了这个错误:
(error: Class 'Column' is not annotated with 'Attribute' )
是因为Doctrine还不支持,还是我漏掉了什么?
这是我想要使用的:
#[ORM\Column(type: "string")]
使用以下内容替换:
/**
* @ORM\Column(type="string")
*/
但我遇到了这个错误:
(error: Class 'Column' is not annotated with 'Attribute' )
是因为Doctrine还不支持,还是我漏掉了什么?
正如@Seb33300所说,在Doctrine ORM 2.9中现在可以这样做。但是对于Symfony,您需要做更多的工作。以下是升级的完整步骤清单:
Upgrade Doctrine ORM: "doctrine/orm": "^2.9"
.
Upgrade Doctrine Bundle: "doctrine/doctrine-bundle": "^2.4"
.
Set doctrine.orm.mappings.App.type: attribute
(by default it's set to 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 App\Repository\DummyRepository;
use Doctrine\ORM\Mapping as ORM;
-/**
- * @ORM\Entity(repositoryClass = DummyRepository::class)
- */
+#[ORM\Entity(repositoryClass: DummyRepository::class)]
class Dummy
{
- /**
- * @ORM\Id
- * @ORM\GeneratedValue
- * @ORM\Column(type = 'integer')
- */
+ #[ORM\Id]
+ #[ORM\GeneratedValue]
+ #[ORM\Column(type: 'integer')]
private $id;
}
Entity
中仍然使用注释,而在 EntAttribs
中(找不到更好的名称lol)则使用属性:https://pastebin.com/VS1jGD8x - the_nuts编辑:Doctrine 2.9现已发布,支持PHP 8属性!
PHP 8注解已经合并到Doctrine ORM的2.9.x
分支中,但该版本尚未发布:
https://github.com/doctrine/orm/pull/8266
以下是有关此功能的文档参考: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/attributes-reference.html