Created
January 17, 2012 12:21
-
-
Save yethee/1626479 to your computer and use it in GitHub Desktop.
Used the SET type of mysql with flagged enumeration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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