urmaul/yii-enumattributes

Yii ActiveRecord behavior used to work with ENUM attributes.

dev-master 2014-06-18 17:08 UTC

This package is not auto-updated.

Last update: 2024-04-27 13:45:59 UTC


README

Yii ActiveRecord behavior used to work with ENUM attributes

Installing

composer require urmaul/yii-enumattributes dev-master

Attaching example

You need to add this behavior to behaviors:

public function behaviors()
{
    return array(
        ...
        'statusEnum' => array(
            'class' => 'EnumAttributesBehavior',
            'attribute' => 'status',
        ),
    );
}

Options

  • attribute (string) - enum attribute name
  • labels (array) - custom labels array (value => label)

Class phpDoc

And also you should add behavior property to phpDoc comments:

/**
 * ...
 * 
 * @property EnumAttributesBehavior $statusEnum
 */
class ...

Using

Values

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.

Labels

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.

Validation rule

This behavior can generate validation rule for you.

public function rules()
{
    return array(
        ...
        $this->statusEnum->rule,
        // or
        $this->statusEnum->rule + array('on' => 'create'),
        ...
    );
}