2017 © Pedro Peláez
 

yii2-extension yii2-option

Yii2 extension to save named value pairs

image

luckynvic/yii2-option

Yii2 extension to save named value pairs

  • Tuesday, March 28, 2017
  • by luckynvic
  • Repository
  • 1 Watchers
  • 0 Stars
  • 36 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

yii2-option

Yii2 extension to save named value pairs in database., (*1)

Installation

Installation is recommended via composer by adding the following to the require section in your composer.json:, (*2)

"luckynvic/yii2-option": "*"

Run composer update afterwards., (*3)

Run migration following command:, (*4)

    yii migrate --migrationPath="@luckynvic/option/migrations"

Configuration

Modify your config, (*5)

    'components' => [
        'option' => ['class'=>'\luckynvic\option\components\OptionComponent'],
        ...
    ]

Usage

Saving Option

$state_list = [
    'O' => 'Open',
    'P' => 'On Progress',
    'C' => 'Complete',
    'A' => 'Cancel',
];
// save all array value
Yii::$app->option->set('state_list', $state_list);

// add new value or change value
Yii::$app->option->set('state_list', 'Failed', 'F');

// save single value
Yii::$app->option->set('app_name', 'Application Name');

Get Option

// get all option list
$state_list = Yii::$app->option->get('state_list');
// get all option list with default if not available
$my_list = Yii::$app->option->get('my_list', null, ['this', 'is', 'my', 'list']);

// get only one value
$progress = Yii::$app->option->get('state_list', 'O');

Note: pass null as index will return entire list., (*6)

Delete option

// delete entire option
Yii::$app->option->delete('state_list'); // or Yii::$app->option->set('state_list', null);

// delete only one item
Yii::$app->option->delete('state_list', 'C'); // or Yii::$app->option->set('state_list', null, 'C');

Note: set value to null make item will be deleted., (*7)

Context

This extension allow option save based on its context. ie option for User., (*8)



class User extends implements IdentityInterface { // add trait use \luckynvic\option\traits\HasOption; // optional to configure context key protected function optionKey() { return 'user-'.$this->id; } } // usage in app $model->findOne(1); // get user background color for user, default blue $color = $model->getOption('background', 'color', 'blue'); // set background color to red $color = $model->getOption('background', 'red', 'color');

Author

  • Lucky Vic (luckynvic@gmail.com)

The Versions

28/03 2017

dev-master

9999999-dev https://github.com/luckynvic/yii2-option

Yii2 extension to save named value pairs

  Sources   Download

BSD-3-Clause

by Lucky Vic

component yii2 option setting

28/03 2017

0.0.3

0.0.3.0 https://github.com/luckynvic/yii2-option

Yii2 extension to save named value pairs

  Sources   Download

BSD-3-Clause

by Lucky Vic

component yii2 option setting

19/08 2016

0.0.2

0.0.2.0 https://github.com/luckynvic/yii2-option

Yii2 extension to save named value pairs

  Sources   Download

BSD-3-Clause

by Lucky Vic

component yii2 option setting

18/08 2016

0.0.1

0.0.1.0 https://github.com/luckynvic/yii2-option

Yii2 extension to save named value pairs

  Sources   Download

BSD-3-Clause

by Lucky Vic

component yii2 option setting