2017 © Pedro PelΓ‘ez
 

yii2-extension yii2-enum

Extension provide very simply use enum for models (and others) in yii2

image

kakadu-dev/yii2-enum

Extension provide very simply use enum for models (and others) in yii2

  • Thursday, June 7, 2018
  • by kakadudev
  • Repository
  • 3 Watchers
  • 1 Stars
  • 69 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

yii2-enum

Extension provide very simply use enum for models (and others) in yii2, (*1)

Installation

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

Either run, (*3)

php composer.phar require --prefer-dist kakadu-dev/yii2-enum "@dev"

or add, (*4)

"kakadu-dev/yii2-enum": "@dev"

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

Usage

Once the extension is installed, simply use it in your code by:, (*6)

Create directory structure for model "User" (only example, not required):, (*7)

common/  
    models/    
        Users/  
            Enums/
                UserStatus.php
            User.php
            UserQuery.php

UserStatus class example:, (*8)

<?php

namespace common\models\Users\Enums;

use Yii;
use Kakadu\Yii2Enum\Enum;

class UserStatus extends Enum
{
    const DELETED = 0;
    const ACTIVE  = 1;

    protected static $attribute = 'status';

    public static function all(): array
    {
        return [
            self::DELETED => Yii::t('app', 'Deleted'),
            self::ACTIVE  => Yii::t('app', 'Active'),
        ];
    }
}

And use:, (*9)

namespace common\models\Users;

use common\models\Users\Enums\UserStatus;

class User extends ActiveRecord
{
    public function rules(): array
    {
        return [
            ['status', 'default', 'value' => UserStatus::ACTIVE],
            ['status', 'in', 'range' => UserStatus::keys()],
        ];
    }
    ...
}

More examples:, (*10)

$model = new User(['status' => UserStatus::ACTIVE]);

if (UserStatus::has($model, UserStatus::ACTIVE)) {
    // do something
}

// DetailView widget (or GridView)
DetailView::widget([
    'model'      => $model,
    'attributes' => [
        'id',
        'name',
        [
            'attribute' => 'status',
            'filter'    => UserStatus::all(),
            'value'     => UserStatus::get($model->status),
        ],
        ...
    ],
])

// In form
$form->field($model, 'status')->dropDownList(UserStatus::all())

Add translation (e.g. in main.php):, (*11)

'i18n'           => [
    'translations' => [
        'kkd-enum*' => \Kakadu\Yii2Enum\EnumTranslation::getConfig(),
    ],
],

That's all. Check it., (*12)

The Versions

07/06 2018

dev-master

9999999-dev

Extension provide very simply use enum for models (and others) in yii2

  Sources   Download

MIT

The Requires

 

by Π―Ρ€ΠΌΠΎΠ»ΡŽΠΊ ΠœΠΈΡ…Π°ΠΈΠ»
by ВимошСнко ΠšΠΎΠ½ΡΡ‚Π°Π½Ρ‚ΠΈΠ½

extension yii2 structure models enum logic

07/06 2018

1.1

1.1.0.0

Extension provide very simply use enum for models (and others) in yii2

  Sources   Download

MIT

The Requires

 

by Π―Ρ€ΠΌΠΎΠ»ΡŽΠΊ ΠœΠΈΡ…Π°ΠΈΠ»
by ВимошСнко ΠšΠΎΠ½ΡΡ‚Π°Π½Ρ‚ΠΈΠ½

extension yii2 structure models enum logic

05/06 2018

1.0

1.0.0.0

Extension provide very simply use enum for models (and others) in yii2

  Sources   Download

MIT

The Requires

 

by Π―Ρ€ΠΌΠΎΠ»ΡŽΠΊ ΠœΠΈΡ…Π°ΠΈΠ»
by ВимошСнко ΠšΠΎΠ½ΡΡ‚Π°Π½Ρ‚ΠΈΠ½

extension yii2 structure models enum logic