Get support for mnapoli/enum-doctrine

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

mnapoli/enum-doctrine

Doctrine integration for myclabs enums

Doctrine integration for myclabs/php-enum via custom Doctrine types.

Build Status Latest Version Total Downloads

This library helps you store Enums in database using Doctrine via custom Doctrine types. Enums must be defined using myclabs/php-enum.

Installation

composer require mnapoli/enum-doctrine

Usage

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;
    }
}

Integer values

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;
    }
}

Custom behavior

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);
    }
}
Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2