You can create a Enum as a type and use it
<?php
namespace AppDBAL;
use DoctrineDBALTypesType;
use DoctrineDBALPlatformsAbstractPlatform;
class EnumColorsType extends Type
{
const ENUM_COLORS = 'enum_colors';
const COLOR_RED = 'red';
const COLOR_BLUE = 'blue';
const COLOR_GREEN= 'green';
publkic static $AVAILBLE_COLORS = ['blue','red','green'];
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return sprintf("ENUM(%s)",implode(',', static::$AVAILBLE_COLORS));
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value;
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (!in_array($value, static::$AVAILBLE_COLORS)) {
throw new InvalidArgumentException("Invalid Colors");
}
return $value;
}
public function getName()
{
return self::ENUM_COLORS;
}
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
}
Then use it
<?php
...
/**
* @ORMEntity
*/
class YourEntity
{
/**
* @Column(type="enum_colors")
*/
private $color;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…