- Allows to add you custom php enums as doctrine type.
- Supports generation of correct alter SQL when updating enum values.
Extension for php-enum (myclabs fork)
Than require package
composer require gusarov112/php-enum-doctrine
Gusarov112\PhpEnumDoctrine\EventListener\EnumUpdaterSubscriber:
tags:
- { name: doctrine.event_subscriber }
class PetEnum extends \Gusarov112\Enum\Enum
{
const CAT = 'CAT';
const DOG = 'DOG';
}
class PetType extends \Gusarov112\PhpEnumDoctrine\DBAL\EnumType
{
public function getEnumClassName(): string
{
return PetEnum::class;
}
public function getName()
{
return 'pet_type';
}
}
use Doctrine\ORM\Mapping as ORM;
class PetEntity {
/**
* @var PetEnum
* @ORM\Column(type="pet_type")
*/
private $type;
public function getType(): PetEnum
{
return $this->type;
}
public function setType(PetEnum $type): self
{
$this->type = $type;
return $this;
}
}
\Doctrine\DBAL\Types\Type::addType('pet_type', PetType::class);
$eventManager = new \Doctrine\Common\EventManager();
$eventManager->addEventSubscriber(new \Gusarov112\PhpEnumDoctrine\EventListener\EnumUpdaterSubscriber());
#!/bin/bash ./vendor/bin/doctrine migrations:diff