Dynamic change Database connection parameter in Yii
before Yii::app()->db is instanced, use this way to changed connectionString, others are the same.
{Code}
Yii::app()->setComponents(array(
'db'=>array('connectionString' => 'mysql:host=yiiblog.info;dbname=BoyLee')
));
{BoyLee Said}
if the db connection is instanced we can use a new db connection, I will test this later, the way i found is to build more than 1 db connection in config/main.php file.
'db'=>array(
'class' => 'CDbConnection',
'connectionString'=>'mysql:host=yiiblog.info;dbname=master',
'charset'=>'utf8',
'username'=>'root',
'password'=>'root',
),
'db2'=>array(
'class' => 'CDbConnection',
'connectionString'=>'mysql:host=yiiblog.info;dbname=slave',
'charset'=>'utf8',
'username'=>'root',
'password'=>'root',
),
Leave Comment