Skip to content

Instantly share code, notes, and snippets.

@nkt
Created June 10, 2014 17:53
Show Gist options
  • Save nkt/0f24505e4cdf78c01a10 to your computer and use it in GitHub Desktop.
Save nkt/0f24505e4cdf78c01a10 to your computer and use it in GitHub Desktop.
Doctrine2 enum type implementation.
<?php
namespace Nkt\Doctrine\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
/**
* Enum type
* @author Gusakov Nikita <[email protected]>
* @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