PHP code example of sergmoro1 / yii2-lookup

1. Go to this page and download the library: Download sergmoro1/yii2-lookup library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sergmoro1 / yii2-lookup example snippets


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

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);
    }
}

<?= Html::a('Properties', ['lookup/property/index']) 

    <?= 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);
                }
            ],

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