jdecool/enum-doctrine

This package is abandoned and no longer maintained. No replacement package was suggested.

Doctrine for enum objects

1.1.0 2020-12-08 21:40 UTC

This package is auto-updated.

Last update: 2022-02-24 20:21:18 UTC


README

Build Status Latest Stable Version

This package provides a base implementation to define doctrine entity column types that are mapped to JDecool\Enum\Enum objects (of jdecool/enum package).

This is a port of acelaya/doctrine-enum-type.

Deprecated

⚠️ This project is no longer actively maintained.

Native enum arrived to PHP in version 8.1: https://www.php.net/enumerations If your project is running PHP 8.1+ or your library has it as a minimum requirement you should use it instead of this library.

Installation

Install it using Composer:

composer require jdecool/enum-doctrine

Usage

This package provides a JDecool\Enum\Doctrine\EnumType class that extends Doctrine\DBAL\Types\Type. You can use it to easily map type names to concrete Enums.

The EnumType class will be used as the doctrine type for every property that is an enumeration.

use JDecool\Enum\Enum;

class MyEnum extends Enum
{
    public const ENUM_1 = 'value_1';
    protected const ENUM_2 = 'value_2';
    private const ENUM_3 = 'value_3';
}

Then, you can map the enum to your entity.

class User
{
    // ...

    /**
     * @var MyEnum
     *
     * @ORM\Column(type=MyEnum::class, length=10)
     */
    protected $action;

    // ...
}

The column type of the property is the FQCN of the MyEnum enum. To get this working, you have to register the concrete column types, using the JDecool\Enum\Doctrine\EnumType::registerEnumType static method.

// in bootstrapping code
use JDecool\Enum\Doctrine\EnumType;

EnumType::registerEnumType(MyEnum::class);

// Don't forget to register the enums for schema operations
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('VARCHAR', MyEnum::class);

Alternatively you can use the JDecool\Enum\Doctrine\EnumType::registerEnumTypes, which expects an array of enums to register.

// ...

use JDecool\Enum\Doctrine\EnumType;

EnumType::registerEnumTypes([
    MyEnum::class,
    'php_enum_type' => MyEnum::class,
]);

If you use Doctrine with Symfony:

# config/packages/doctrine.yaml
doctrine:
    dbal:
        types:
            uuid: JDecool\Enum\Doctrine\EnumType