如何在教义中使用php8属性而不是注释?
2022-08-30 18:46:46
这是我想用的:
#[ORM\Column(type: "string")]
而不是这样:
/**
* @ORM\Column(type="string")
*/
但是我得到这个错误:
(error: Class 'Column' is not annotated with 'Attribute' )
是因为教义还不支持它,还是我错过了什么?
这是我想用的:
#[ORM\Column(type: "string")]
而不是这样:
/**
* @ORM\Column(type="string")
*/
但是我得到这个错误:
(error: Class 'Column' is not annotated with 'Attribute' )
是因为教义还不支持它,还是我错过了什么?
正如@Seb33300所说,是的,现在在教义ORM 2.9中是可能的。但对于Symfony来说,你需要做的远不止于此。以下是升级步骤的完整列表:
升级原则 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 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;
}
编辑:原则2.9现已发布,支持PHP 8属性!
PHP 8 注释已合并到尚未发布的 Doctrine ORM 分支中:https://github.com/doctrine/orm/pull/82662.9.x
以下是与此功能相关的文档参考:https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/attributes-reference.html