How can i use php8 attributes instead of annotations in doctrine?
Solution 1
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:
-
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 toannotation
):# 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; }
Solution 2
EDIT: Doctrine 2.9 is now released with PHP 8 attributes support!
PHP 8 annotations have been merged in Doctrine ORM 2.9.x
branch which is not released yet:
https://github.com/doctrine/orm/pull/8266
Here is the documentation reference related to this feature: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/attributes-reference.html
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 19, 2022Comments
-
Admin about 2 years
This is what I would like to use:
#[ORM\Column(type: "string")]
instead of this:
/** * @ORM\Column(type="string") */
But I'm getting this error:
(error: Class 'Column' is not annotated with 'Attribute' )
Is it because Doctrine does not support it yet, or am I missing something?
-
Jason Aller about 3 yearsThe change from "annotation" to "attribute" is critical. Thank you for documenting it.
-
the_nuts about 2 yearsIs there any way to allow both, to have a smooth migration for the existing attributes?