Skip to content

Instantly share code, notes, and snippets.

@yethee
Created January 17, 2012 12:21
Show Gist options
  • Save yethee/1626479 to your computer and use it in GitHub Desktop.
Save yethee/1626479 to your computer and use it in GitHub Desktop.
Used the SET type of mysql with flagged enumeration
<?php
class FooSetType extends \Doctrine\DBAL\Types\Type
{
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return "SET('one','two','three') NOT NULL";
}
public function getName()
{
return 'foo_set';
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value !== null) {
return FooSetEnum::create((int)$value);
}
return null;
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (!$value instanceof FooSetEnum) {
throw new \InvalidArgumentException('Invalid value.');
}
return $value->getValue();
}
public function convertToPHPValueSQL($sqlExpr, $platform)
{
return $sqlExpr . '+0';
}
}
<?php
class FooSetEnum extends \Biplane\EnumBundle\Enumeration\FlaggedEnum
{
const ONE = 1;
const TWO = 2;
const THREE = 4;
static public function getPossibleValues()
{
return array(self::ONE, self::TWO, self::THREE);
}
static public function getReadables()
{
return array(
self::ONE => 'one',
self::TWO => 'two',
self::THREE => 'three',
);
}
static protected function getBitmask()
{
return 7;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment