2017 © Pedro Peláez
 

yii2-extension yii2-constant-label

Create labels for any model constants

image

ancor/yii2-constant-label

Create labels for any model constants

  • Saturday, March 12, 2016
  • by ancor.dev
  • Repository
  • 1 Watchers
  • 0 Stars
  • 82 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

Allow to specify labels for constants in a Model

Feel free to let me know what else you want added via:, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

$ php composer.phar require ancor/yii2-constant-label

or add, (*4)

"ancor/yii2-constant-label": "dev-master"

to the require section of your composer.json file., (*5)

Adding to the model

To use ConstantLabelBehavior, insert the following code to your Model class:, (*6)

use common\behaviors\ConstantLabelBehavior;

/**
 * @mixin ConstantLabelBehavior
 */
class MyModel extends Model
{
    const STATUS_ACTIVE  = 10;
    const STATUS_DELETED = 0;

    public function behaviors()
    {
      return [
          [
              'class' => ConstantLabelBehavior::className(),
              'constantLabels' => [
                  'status' => [
                      self::STATUS_ACTIVE  => 'User is active',
                      self::STATUS_DELETED => 'User deleted',
                  ]
              ],
          ]
      ];
    }
}

Usage

$model = new MyModel();


// return key-value array with constant values as key and constant label as value
$labels = $model->getConstantLabels('status');

// return label for one constant
$label = $model->getConstant('status', $model::STATUS_ACTIVE);

// return values of all constants
$values = $model->getConstantValues('status'); // [10, 0]

// also it can be use in validation rules
[
    ['status', 'in', 'range' => $model->getConstantValues('status')],
]

The Versions

12/03 2016

dev-master

9999999-dev

Create labels for any model constants

  Sources   Download

GPLv3

The Requires

 

by Anton Korniychuk