Skip to content

urmaul/yii-enumattributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

Yii Enumattributes

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'),
        ...
    );
}

About

Yii ActiveRecord behavior used to work with ENUM attributes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages