Yii Get MySql Enum Value

Yii 6 ธ.ค. 2013

Create file EnumData.php put in compoment


class EnumData extends CHtml
{
public static function enumItem($model,$attribute)
{
$attr=$attribute;
self::resolveName($model,$attr);
preg_match('/((.*))/',$model->tableSchema->columns[$attr]->dbType,$matches);
foreach(explode(',', $matches[1]) as $value)
{
$value=str_replace("'",null,$value);
$values[$value]=Yii::t('enumItem',$value);
}

return $values;
}

public static function enumDropDownList($model, $attribute, $htmlOptions)
{
return CHtml::activeDropDownList( $model, $attribute,ZHtml::enumItem($model, $attribute), $htmlOptions);
}
}

his allow you to write in the view (without any change in the model):

or , more tasty:

ที่มา http://www.yiiframework.com/forum/index.php/topic/10079-enum-db-type-in-yii/

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.