Home > Software design >  How to convert symfony gedmo annotation to PHP 8 Attributes
How to convert symfony gedmo annotation to PHP 8 Attributes

Time:02-04

I'm working on Symfony 6 PHP 8 project and i'm using gedmo doctrine extension .

I can't find a full documentation about converting gedmo annotation to PHP 8 Attributes.

I'm tring to convert like this :

/**
 * @ORM\Column(type="string", length=255)
 * @Gedmo\Slug(fields={"title"})
 */
private $slug;

#[Gedmo\Slug(fields: title)]

but it doesn't work !

How can i use gedmo with PHP 8 Attributes ?

CodePudding user response:

Rector supports this. The rule comes from rector-doctrine, which is included with the standard install method.

Follow the rector install guide and edit the rector.php config to add the required rules.

Rector also has rule sets for updating Symfony to see rector-symfony.

Example of converting the Doctrine and Gedmo annotation to PHP 8 attributes.

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;

return static function (RectorConfig $rectorConfig): void {

    $rectorConfig->sets([
        DoctrineSetList::DOCTRINE_CODE_QUALITY,
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        DoctrineSetList::GEDMO_ANNOTATIONS_TO_ATTRIBUTES,
    
    ]);
};

e.g. converts this

/**
 * @Gedmo\Slug(fields={"title"})
 * @ORM\Column(length=128, unique=true)
 */
private $slug;

to

#[ORM\Column(length: 128, unique: true)]
#[Gedmo\Slug(fields: ['title'])]
private $slug;

CodePudding user response:

you'll have to do the whole class property with attributes. the fields property at slug needs to be an array.

#[\Gedmo\Mapping\Annotation\Slug(fields: ['name'])]
#[\Doctrine\ORM\Mapping\Column(
    type: 'string',
    length: 255,     
)]
  • Related