PHP code example of luckynvic / yii2-option

1. Go to this page and download the library: Download luckynvic/yii2-option 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/ */

    

luckynvic / yii2-option example snippets


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

$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 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');

// 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');



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->setOption('background', 'red', 'color');