PHPackages                             porcelanosa/yii2-options - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. porcelanosa/yii2-options

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

porcelanosa/yii2-options
========================

Yii2 Extenstion for set options

015PHP

Since Aug 1Pushed 9y ago2 watchersCompare

[ Source](https://github.com/porcelanosa/yii2-options)[ Packagist](https://packagist.org/packages/porcelanosa/yii2-options)[ RSS](/packages/porcelanosa-yii2-options/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

**WARNING! UNDER DEVELOPMENT**[![Total Downloads](https://camo.githubusercontent.com/4363e25c71f262a8320e951ec118cf947912780218680db5c42c11466e5adf6c/68747470733a2f2f706f7365722e707567782e6f72672f706f7263656c616e6f73612f796969322d6f7074696f6e732f646f776e6c6f616473)](https://packagist.org/packages/porcelanosa/yii2-options)Installation
===============================================================================================================================================================================================================================================================================================================================================

[](#warning-under-developmentinstallation)

This document will guide you through the process of installing yii2-options using **composer**. Installation is a quick and easy several step process.

> **NOTE:** Before we start make sure that you have properly configured **db** application component.

Step 1: Download using composer
-------------------------------

[](#step-1-download-using-composer)

Add yii2-options to the require section of your **composer.json** file:

```
{
    "require": {
        "porcelanosa/yii2-options": "dev-master"
    }
}
```

And run following command to download extension using **composer**:

```
$ php composer.phar update
```

Step 2: Configure your application
----------------------------------

[](#step-2-configure-your-application)

Add options module to both web and console config files as follows:

```
...
'modules' => [
    ...
    'options' => [
        'class' => 'porcelanosa\yii2options\Module',
        'layout' => '@app/modules/admin/views/layouts/main',
        'model_path' => '@app/modules/admin/models/*.php', // models php files
        'modelNamespace' => 'app\modules\admin\models\', // models namespace
        'fileUrl'        => '/storage/uploads/richtext/files',
        'filePath'       => '@storage/uploads/richtext/files',
        'imageUrl'       => '/storage/uploads/richtext/images',
        'imagePath'      => '@storage/uploads/richtext/images',
    ],
    ...
],
...
```

Configure request parser

```
'components'     => [
    'request' => [
        'parsers' => [
            'application/json' => 'yii\web\JsonParser',
        ]
    ],
```

Configure Karik-V module

```
'modules' => [
   'gridview' =>  [
        'class' => '\kartik\grid\Module'
    ]
],
```

Step 3: Updating database schema
--------------------------------

[](#step-3-updating-database-schema)

After you downloaded and configured yii2-options, the last thing you need to do is updating your database schema by applying the migration:

```
$ php yii migrate/up --migrationPath=@vendor/porcelanosa/yii2-options/migrations
```

Menu items

```
['label' => Yii::t('app', 'ADMIN_NAV_STATUS_TYPES'), 'url' => ['/options/optiontypes/index']],
['label' => Yii::t('app', 'ADMIN_NAV_OPTIONS_LIST'), 'url' => ['/options/optionslist/index']],
```

Step 4: Adjust models
---------------------

[](#step-4-adjust-models)

Add behavior

```
use porcelanosa\yii2options\models\Options;
use porcelanosa\yii2options\OptionsBehavior;
use porcelanosa\yii2options\ChildOptionsBehavior;
use porcelanosa\yii2options\components\helpers\MyHelper;

public function behaviors()
{
    return [
        'optionsBehavior' => [
           'class' => OptionsBehavior::className(),
           'model_name' => $this::className(), // convert className to model name without namespace
           'uploadImagePath' => Yii::getAlias( '@webroot' ) . '/uploads/cats/', // alias of upload folder
           'uploadImageUrl' => Yii::getAlias( '@web' ) . '/uploads/cats/', // alias of upload folder

            // admin application url without end slash
            'appUrl'                 => '/backend'
        ],
}
```

For Child behavior for example in Items model add:

```
'childOptionsBehavior' => [
    'class' => ChildOptionsBehavior::className(),
    'model_name' => $this::className(),
    'parent_model_name' => '\common\models\Cats',
    // relation name for parent model, e.q. if relation function is getCat() - relation name is "cat"
    'parent_relation'   => 'cat',
    'uploadImagePath' => Yii::getAlias( '@storage' ) . '/uploads/items/', // alias of upload folder
    'uploadImageUrl' => '/storage/uploads/items/', // Yii::getAlias( '@storageUrl' ) . alias of upload folder
    // admin application url without end slash
    'appUrl'                 => '/backend'
],
```

Add binding paramters

```
public $modelFrontName = 'Категории'; //if not define $modelFrontName - not show in dropdown list in optionslist controller

// in Parent model define Child model
public $childModels = [
    'Items'=>'Товары в категории',
];
```

Step 5: Show options in admin view
----------------------------------

[](#step-5-show-options-in-admin-view)

```
