Giter VIP home page Giter VIP logo

enum-doctrine's Introduction

Doctrine Enum Type

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

enum-doctrine's People

Contributors

jdecool avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.