2017 © Pedro Peláez
 

library php-enum-doctrine

Doctrine PHP enum support

image

zlikavac32/php-enum-doctrine

Doctrine PHP enum support

  • Friday, January 26, 2018
  • by zlikavac32
  • Repository
  • 1 Watchers
  • 1 Stars
  • 180 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 3 % Grown

The README.md

PHP Enum Doctrine

Build Status Latest Stable Version License Scrutinizer Code Quality Codacy Badge, (*1)

Doctrine support for zlikavac32/php-enum., (*2)

Table of contents

  1. Installation
  2. Usage
    1. Custom column length
    2. Custom representation
  3. Limitations
  4. Further work

Installation

Recommended installation is through Composer., (*3)

composer require zlikavac32/php-enum-doctrine

Usage

Assumption is that there exists a valid enum \YesNoEnum., (*4)

Create a new type that extends \Zlikavac32\DoctrineEnum\DBAL\Types\EnumType., (*5)

use Zlikavac32\DoctrineEnum\DBAL\Types\EnumType;

class YesNoEnumType extends EnumType 
{
    // ...
}

Next, define protected function enumClass(): string. This method should return FQN of the enum class that this type exposes to the Doctrine., (*6)

protected function enumClass(): string
{
    return \YesNoEnum::class;
}

Define Doctrine method public function getName(): string that defines type's name., (*7)

public function getName(): string
{
    return 'enum_yes_no';
}

And that's it. Only thing left to do is to register the type using, (*8)

\Doctrine\DBAL\Types\Type::addType('enum_yes_no', \YesNoEnumType::class);

You can now use enum_yes_no type., (*9)

/**
 * @Column(type="enum_yes_no", nullable=true)
 * @var \YesNoEnum|null
 */
private $yesNo;

For more info on the custom Doctrine mapping types, check official documentation., (*10)

Custom column length

Internally this library uses varchar type with the maximum length of 32. If you want to fit the length to your own needs, just override method protected function columnLength(): int., (*11)

protected function columnLength(): int
{
    return 16;
}

Note that on types first usage, all enum elements names are checked against specified column length. If a name longer than maximum length is detected, a \LogicException is thrown., (*12)

Custom representation

By default, name of the enum element is used for it's representation in the database. To change that behaviour, override methods enumToDatabaseValue() and databaseValueToEnum()., (*13)

Limitations

This library does not use platform dependent types like enum in MySQL or custom types in PostgresSQL. Instead, varchar is used., (*14)

Reasons for this are:, (*15)

  • Doctrine can not diff enum contents because that's types intrinsic property
  • for PostgresSQL we can't diff column because type is not in Doctrine control
  • column constraints can not be used because they break ALTER syntax

If you know how to avoid any of this, please let me know., (*16)

Further work

Figure out how to overcome issues in Limitations., (*17)

The Versions

26/01 2018

dev-master

9999999-dev

Doctrine PHP enum support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijan Šuflaj

doctrine enum php-enum enumeration doctrine-enum

17/11 2017

0.1.0

0.1.0.0

Doctrine PHP enum support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijan Šuflaj

doctrine enum php-enum enumeration doctrine-enum