shiyang
(Dr Pioge)
January 24, 2015, 11:13pm
1
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?
samdark
(Alexander Makarov)
January 26, 2015, 7:27pm
2
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.
uEhlO4a
(Y Korotia)
January 27, 2015, 1:02am
3
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
dniznick
(Dniznick)
January 27, 2015, 5:34am
4
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.
shiyang
(Dr Pioge)
January 27, 2015, 2:00pm
6
thanks.I just want to find a way that can improve performance.