Doctrine integration for myclabs/php-enum via custom Doctrine types.
This library helps you store Enums in database using Doctrine via custom Doctrine types. Enums must be defined using myclabs/php-enum.
composer require mnapoli/enum-doctrine
Given the following enum:
class Currency extends Enum
{
private const DOLLAR = 'dollar';
private const EURO = 'euro';
}
You will need to write a custom Doctrine type that inherits StringEnumType
or IntegerEnumType
:
class CurrencyType extends \MyCLabs\Enum\Doctrine\StringEnumType
{
public function getName(): string
{
return 'currency';
}
public function getClassName(): string
{
return Currency::class;
}
}
You then need to register the custom type (see the Doctrine documentation):
Type::addType('currency', 'App\Type\CurrencyType');
$conn = $em->getConnection();
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('db_currency', 'currency');
In Symfony (see the Symfony documentation):
# config/packages/doctrine.yaml
doctrine:
dbal:
types:
currency: 'App\Type\CurrencyType'
The type can now be used in Doctrine, for example in entities:
class Foo
{
/**
* @var Currency
* @ORM\Column(type="currency")
*/
private $currency;
public function getCurrency(): Currency
{
return $this->currency;
}
public function setCurrency(Currency $currency)
{
$this->currency = $currency;
}
}
If your Enum uses int
values in the database like this one:
class Currency extends Enum
{
private const DOLLAR = 1;
private const EURO = 2;
}
you will need to extend IntegerEnumType
instead:
class CurrencyType extends IntegerEnumType
{
public function getName(): string
{
return 'currency';
}
public function getClassName(): string
{
return Currency::class;
}
}
When mapping entities to a legacy database we sometimes have to deal with weird values outside of our control. In this case feel free to override methods in the parent class.
Here is an example where we force empty strings in the database to be turned into null
in PHP:
class CurrencyType extends \MyCLabs\Enum\Doctrine\StringEnumType
{
...
public function convertToPHPValue($value, AbstractPlatform $platform)
{
// We map the '' value (in database) to `null` in the PHP code
if ($value === '') {
return null;
}
// Everything else is handled as usual
return parent::convertToPHPValue($value, $platform);
}
}