ouhman
(Ouhman)
1
Hello all Yii folks,
I am quite surprised as I do not find any way of fetching enum values from Mysql in an Active record class.
I found a topic on the forum here (http://www.yiiframework.com/forum/index.php?/topic/6456-handle-enum-field-from-db-with-yii/) about getting those values, but the code is quite huge for that.
Any chance to have a fetchEnumValues in a near future?
Cheers,
zaccaria
(Matteo Falsitta)
2
Hi.
I am facing the same problem too, and I wrote workaround for it.
Is quite similar to the one you posted, just a bit more general:
public static function enumItem($model,$attribute)
{
$attr=$attribute;
self::resolveName($model,$attr);
$enum=$model->tableSchema->columns[$attr]->dbType;
$off=strpos($enum,"(");
$enum=substr($enum, $off+1, strlen($enum)-$off-2);
$keys=str_replace("'",null,explode(",",$enum));
for($i=0;$i<sizeof($keys);$i++)
$values[$keys[$i]]=Yii::t('enumItem',$keys[$i]);
return $values;
}
This can be used for popolate a dropDownList like that:
CHtml::activeDropDownList($model,$attribute,
ZHtml::enumItem($model, $attribute),
$htmlOptions);
ZHtml is my extension of CHtml.
I hope it helps!