Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mapping type ENUMS in Doctrine?

I am using PHP 8.1.6, Symfony 6 with Doctrine ORM. I've been trying to get it to work for a long time but with no luck. I have followed tutorials but they are usually from other older versions of Symfony or Doctrine and those options do not work, I will thank greatly if someone can give me a little example to be able to make it work and understand it better.

like image 365
RookieCoder Avatar asked Oct 22 '25 20:10

RookieCoder


1 Answers

  1. You need to create your enum,

  2. Please create your mapping class in the type directory:

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        $enumClass = $this->getEnum();
    
        return $enumClass::from($value);
    }
    
    
    public function convertToDatabaseValue($enum, AbstractPlatform $platform)
    {
        return $enum->value;
    }
    
  3. Please register in doctrine.yaml in your dbal types

        types: 
             enumType: App\Doctrine\DBAL\Type\EnumType
    
  4. In your Entity, you need to put the enumType in the orm column and enum in your Entity type

like image 122
HAMON Célian Avatar answered Oct 24 '25 09:10

HAMON Célian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!