2017 © Pedro Peláez
 

yii2-extension yii2-lookup

Dictionary for seldom changed items.

image

sergmoro1/yii2-lookup

Dictionary for seldom changed items.

  • Thursday, July 19, 2018
  • by sergmoro1
  • Repository
  • 1 Watchers
  • 0 Stars
  • 20 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 18 % Grown

The README.md

Yii2 module for lookup enumeration values

Advantages

Fields can has enumeration values. For exaple for a post status:, (*1)

'status' => [
    1 => 'Draft', 
    2 => 'Published',
    3 => 'Archived',
]

If the number of enumerable values is limited, it is convenient to store them in one table., (*2)

Values for modules can be added by migrations or by interface., (*3)

Installation

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

Either run, (*5)

composer require --prefer-dist sergmoro1/yii2-lookup, (*6)

or add, (*7)

"sergmoro1/yii2-lookup": "^1.1", (*8)

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

Run migration php yii migrate --migrationPath=@vendor/sergmoro1/yii2-lookup/src/migrations, (*10)

Usage

Values for modules can be added by migrations, (*11)

use yii\db\Migration;

class m180116_073828_lookup_fill extends Migration
{
    const TABLE_LOOKUP   = '{{%lookup}}';
    const TABLE_PROPERTY = '{{%property}}';

    const USER_ROLE   = 1;
    const USER_STATUS = 2;

    public function safeUp()
    {
        $this->insert(self::TABLE_PROPERTY, ['id' => self::USER_ROLE, 'name' => 'UserRole']);
        $this->insert(self::TABLE_LOOKUP, ['name' => 'Admin',       'code' => 1, 'property_id' => self::USER_ROLE, 'position' => 1]);
        $this->insert(self::TABLE_LOOKUP, ['name' => 'Author',      'code' => 2, 'property_id' => self::USER_ROLE, 'position' => 2]);
        $this->insert(self::TABLE_LOOKUP, ['name' => 'Commentator', 'code' => 3, 'property_id' => self::USER_ROLE, 'position' => 3]);

        $this->insert(self::TABLE_PROPERTY, ['id' =>  self::USER_STATUS, 'name' => 'UserStatus']);
        $this->insert(self::TABLE_LOOKUP, ['name' => 'Active',  'code' => 1, 'property_id' => self::USER_STATUS, 'position' => 1]);
        $this->insert(self::TABLE_LOOKUP, ['name' => 'Archive', 'code' => 2, 'property_id' => self::USER_STATUS, 'position' => 2]);
    }

    public function safeDown()
    {
        $this->delete(self::TABLE_LOOKUP, 'property_id=' . self::USER_ROLE);
        $this->delete(self::TABLE_LOOKUP, 'property_id=' . self::USER_STATUS);
        $this->delete(self::TABLE_PROPERTY, self::USER_ROLE);
        $this->delete(self::TABLE_PROPERTY, self::USER_STATUS);
    }
}

or by interface and then place the links in a menu or sidebar., (*12)

<?= Html::a('Properties', ['lookup/property/index']) ?>
<?= Html::a('Property\'s values', ['lookup/lookup/index']) ?>

To get all items by property., (*13)

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'layout' => "{items}\n{summary}\n{pager}",
        'columns' => [
            'username',
            'email',
            [
                'attribute' => 'status',
                'filter' => Lookup::items('UserStatus'),
                'value' => function($data) {
                    return Lookup::item('UserStatus', $data->status);
                }
            ],

To get the name of concrete item., (*14)

Lookup::item('PostStatus', $data->status);

The Versions

19/07 2018

dev-master

9999999-dev

Dictionary for seldom changed items.

  Sources   Download

MIT

The Requires

 

by Sergey Morozov

yii2 dictionary lookup