Yii ActiveRecord behavior used to work with ENUM attributes
composer require urmaul/yii-enumattributes dev-master
You need to add this behavior to behaviors:
public function behaviors()
{
return array(
...
'statusEnum' => array(
'class' => 'EnumAttributesBehavior',
'attribute' => 'status',
),
);
}
- attribute (string) - enum attribute name
- labels (array) - custom labels array (value => label)
And also you should add behavior property to phpDoc comments:
/**
* ...
*
* @property EnumAttributesBehavior $statusEnum
*/
class ...
Now you can retrieve list of possible values by calling behavior like that:
$model->statusEnum->values
Values order is equal to values order in DB structure.
Or you can retrieve a map "value => label" by calling behavior like that:
$model->statusEnum->valueLabels
Values order is equal to values order in DB structure.
Labels are generated from values using CModel::generateAttributeLabel function. You can set custom labels using the labels behavior option.
This behavior can generate validation rule for you.
public function rules()
{
return array(
...
$this->statusEnum->rule,
// or
$this->statusEnum->rule + array('on' => 'create'),
...
);
}