ancor / yii2-constant-label
Create labels for any model constants
Package info
github.com/ancor-dev/yii2-constant-label
Type:yii2-extension
pkg:composer/ancor/yii2-constant-label
dev-master
2016-03-12 14:03 UTC
Requires
- yiisoft/yii2: ^2.0
This package is not auto-updated.
Last update: 2026-03-15 02:56:55 UTC
README
Feel free to let me know what else you want added via:
Installation
The preferred way to install this extension is through composer.
Either run
$ php composer.phar require ancor/yii2-constant-label
or add
"ancor/yii2-constant-label": "dev-master"
to the require section of your composer.json file.
Adding to the model
To use ConstantLabelBehavior, insert the following code to your Model class:
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')], ]