Enum Type for Doctrine
The recommended way to install is through Composer:
composer require jungle-bay/doctrine-enum-type
<?php
namespace Acme\Types;
use Doctrine\DBAL\Types\EnumType;
class SexType extends EnumType {
const NAME = 'sex_type';
const MAN_VALUE = 'MAN';
const WOMAN_VALUE = 'WOMAN';
protected function getValue() {
return array(
self::MAN_VALUE,
self::WOMAN_VALUE
);
}
public function getName() {
return self::NAME;
}
}
<?php
namespace Acme\Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*
* @ORM\Table(
* name = "users"
* )
*/
class User {
/**
* @ORM\Column(
* type = "sex_type"
* )
*/
private $sex;
}
Do not forget to register the type!
\Doctrine\DBAL\Types\Type::addType(SexType::NAME, SexType::class); /** @var \Doctrine\DBAL\Connection $conn */ $conn->getDatabasePlatform()->registerDoctrineTypeMapping('sex', SexType::NAME);
This bundle is under the MIT license. See the complete license in the file: here.