27

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?

2
  • What use-case do you have for needing to use annotations like that? What are you trying to solve?
    – hppycoder
    Mar 23, 2021 at 20:32
  • 3
    In case you have hundreds of annotations and want to convert them attributes, you might prefer to automate this. I wrote an open-source tool Rector that handles this upgrade for you: getrector.org/blog/how-to-upgrade-annotations-to-attributes Oct 13, 2021 at 19:22

2 Answers 2

62

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. Upgrade Doctrine ORM: "doctrine/orm": "^2.9".

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

  3. Set doctrine.orm.mappings.App.type: attribute (by default it's set to annotation):

    # config/packages/doctrine.yaml
    
    doctrine:
      orm:
        mappings:
          App:
            type: attribute
    
  4. 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;
     }
    
4
  • 18
    The change from "annotation" to "attribute" is critical. Thank you for documenting it. Jun 10, 2021 at 1:06
  • 6
    Is there any way to allow both, to have a smooth migration for the existing attributes?
    – the_nuts
    Apr 10, 2022 at 16:51
  • @the_nuts Could you able to find a way to allow both ? Please let me know
    – nas
    Dec 15, 2022 at 12:39
  • 1
    @nas yes, I created another directory, so what I have in Entity is still with annotation, and what I have in EntAttribs (couldn't find a better name lol) is with attributes: pastebin.com/VS1jGD8x
    – the_nuts
    Dec 19, 2022 at 14:16
15

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.