Enum is mysql specific, so, you cannot have an enum in your schema.xml.
If you don't want to create another table to store the different
possible values, you can simulate an enum very easily.
Imagine you have a status column in you article table you want to be an
enum with values like ('open', 'closed'). Simply declare the status
column as integer and then (not tested):
In your ArticlePeer.php class:
class ArticlePeer...
{
static protected $STATUS_INTEGERS = array('open', 'closed');
static protected $STATUS_VALUES = null;
static public function getStatusFromIndex($index)
{
return self::$STATUS_INTEGERS[$index];
}
static public function getStatusFromValue($value)
{
if (!self::$STATUS_VALUES)
{
self::STATUS_VALUES = array_flip(self::$STATUS_INTEGERS);
}
$values = strtolower($value);
if (!isset(self::STATUS_VALUES[$value])
{
throw new PropelException(sprintf('Status cannot take "%s" as a
value', $value));
}
return self::STATUS_VALUES[strtolower($value)];
}
}
In your Article.php class:
class Article
{
public function setStatusName($value)
{
$this->setStatus(self::getStatusFromValue($value));
}
public function getStatusName()
{
return self::getStatusFromIndex($this->getStatus());
}
}
Fabien
Dealing with ENUM with Symfony, Propel and Mysql
最新推荐文章于 2019-07-02 16:06:06 发布
本文介绍了一种在不使用MySQL特有枚举类型的情况下,在PHP中通过定义静态数组和方法来模拟枚举类型的方法。这种方法适用于希望保持数据一致性但又不想创建额外表存储枚举值的情况。

2277

被折叠的 条评论
为什么被折叠?



