Yii2 Params From Database. Yii::app->params[]

my code(in common/config/params.php):

$connection = new \yii\db\Connection([

'dsn' => 'mysql:host=localhost;dbname=blog',


 'username' => 'root',


 'password' => 'adminadmin',

]);

$connection->open();

$options = $connection->createCommand('SELECT * FROM pre_options')->queryAll();


$connection->close();


foreach ($options as $key => $row) {


		$option[$row['option_name']] = $row['option_value'];


	}


return $option;

in this way I can add params (Yii::$app->params[‘value’]) from database.

but I don’t think it was a good way, like how can i use cache? Do you have any better way to add params from database?

If you need params in config the it’s probably the only way. If these to be used in app you can just create AR model storing a single key-value and use it.

i would use bootstraping with exporting params into file or something, and then merging (if not too many options)

or set in bootstrap $app->params = some\AR\model\with\caching;

with custom method get(‘key’)

and then use in code like

Yii::$app->params->get(‘your key’);

but with such success you can just make custom component

problem is, params can contain very complex data

Look at this:

https://github.com/phemellc/yii2-settings

This is an extension that defines basic getters/setters for retriving settings, with caching built in.

thanks. I will try it.

thanks.I just want to find a way that can improve performance.