2017 © Pedro Peláez
 

library yii-enumattributes

Yii ActiveRecord behavior used to work with ENUM attributes.

image

urmaul/yii-enumattributes

Yii ActiveRecord behavior used to work with ENUM attributes.

  • Wednesday, June 18, 2014
  • by urmaul
  • Repository
  • 1 Watchers
  • 0 Stars
  • 43 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Yii Enumattributes

Yii ActiveRecord behavior used to work with ENUM attributes, (*1)

Installing

composer require urmaul/yii-enumattributes dev-master

Attaching example

You need to add this behavior to behaviors:, (*2)

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:, (*3)

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

Using

Values

Now you can retrieve list of possible values by calling behavior like that:, (*4)

$model->statusEnum->values

Values order is equal to values order in DB structure., (*5)

Labels

Or you can retrieve a map "value => label" by calling behavior like that:, (*6)

$model->statusEnum->valueLabels

Values order is equal to values order in DB structure., (*7)

Labels are generated from values using CModel::generateAttributeLabel function. You can set custom labels using the labels behavior option., (*8)

Validation rule

This behavior can generate validation rule for you., (*9)

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

The Versions

18/06 2014

dev-master

9999999-dev https://github.com/urmaul/enumattributes

Yii ActiveRecord behavior used to work with ENUM attributes.

  Sources   Download

MIT

The Requires

 

yii behavior enum