Skip to content

Instantly share code, notes, and snippets.

@nkt
Created Jun 10, 2014
Embed
What would you like to do?
Doctrine2 enum type implementation.
<?php
namespace Nkt\Doctrine\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
/**
* Enum type
* @author Gusakov Nikita <dev@nkt.me>
* @license MIT
*/
class EnumType extends Type
{
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
if (!isset($fieldDeclaration['variants']) || !is_array($fieldDeclaration['variants'])) {
throw new \InvalidArgumentException('You have to define array of variants');
}
$variants = array_map(function ($variant) use ($platform) {
return "'$variant'";
}, $fieldDeclaration['variants']);
return 'ENUM(' . join(', ', $variants) . ')';
}
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
public function getName()
{
return 'enum';
}
}
@nkt
Copy link
Author

nkt commented Jun 10, 2014

Add next code in your app/config/config.yml

doctrine:
    dbal:
        ...
        mapping_types:
            enum: string
        types:
            enum: Nkt\Doctrine\Type\EnumType

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment