Giter VIP home page Giter VIP logo

enum-doctrine's Introduction

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

enum-doctrine's People

Contributors

mnapoli avatar

Stargazers

Felipe Sayão Lobato Abreu avatar

Watchers

 avatar James Cloos 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.