/doctrine-enum-php-type

Added a doctrine type for the php enumeration system designed to be as close as possible to PHP 8.1 for earlier versions.

Primary LanguagePHPApache License 2.0Apache-2.0

Doctrine Enum PHP Type


Read the enum php doc:

https://github.com/Neutron-Pro/enum-php


Installation

composer require neutronstars/doctrine-enum-php-type


Doctrine Configuration

If you use it, you must create a Type class for your enum:

namespace App\Types;

class MyEnumType extends \NeutronStars\Enum\Types\EnumType {
  public const MY_ENUM = 'my_enum';
  
  public function getName(): string
  {
    return self::MY_ENUM;
  }
  
  public function convertToPHPValue($value,\Doctrine\DBAL\Platforms\AbstractPlatform $platform): MyEnum
  {
    return MyEnum::from($value);
  }
}

You must add this to the doctrine.yaml configuration file:

doctrine:
  dbal:
    types:
      my_enum:
        class: App\Types\MyEnumType

On the annotation that contains the field of your entity, you must put the type:

/**
 * @ORM\Column(type="my_enum")
 * @var MyEnum|null
 */
private ?MyEnum $myEnum;

you must comment out the column that contains the enum during your sql migration:

$this->addSql('COMMENT ON COLUMN "your_table"."your_column" IS \'(DC2Type:my_enum)\';');