28

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?

Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115
glm
  • 393
  • 1
  • 3
  • 6
  • What use-case do you have for needing to use annotations like that? What are you trying to solve? – hppycoder Mar 23 '21 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: https://getrector.org/blog/how-to-upgrade-annotations-to-attributes – Tomas Votruba Oct 13 '21 at 19:22

2 Answers2

65

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;
     }
    
Denis V
  • 3,290
  • 1
  • 28
  • 40
  • 19
    The change from "annotation" to "attribute" is critical. Thank you for documenting it. – Jason Aller Jun 10 '21 at 01:06
  • 6
    Is there any way to allow both, to have a smooth migration for the existing attributes? – the_nuts Apr 10 '22 at 16:51
  • @the_nuts Could you able to find a way to allow both ? Please let me know – nas Dec 15 '22 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: https://pastebin.com/VS1jGD8x – the_nuts Dec 19 '22 at 14:16
16

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

Seb33300
  • 7,464
  • 2
  • 40
  • 57