Reputation: 548
I have a simple select
in my View code. It represents list of cities and on my server there're several databases which are responsible for each city. I have my Model code and it takes city_id
. Depends on it I want to connect to database and seek for needed data in it. I've added second database to my components like:
'db' => require(__DIR__ . '/db.php'),
'db2' => require(__DIR__ . '/db_login.php'),
and two files which returns database connection.
File 1:
'class' => 'yii\db\Connection',
'dsn' => $dsn,
'username' => $username,
'password' => $password,
'charset' => 'utf8',
File 2:
'class' => 'yii\db\Connection',
'dsn' => $dsn,
'username' => $username,
'password' => $password,
'charset' => 'utf8',
I want to change my database name somehow dynamically right after users' choice.
Upvotes: 0
Views: 1169
Reputation: 133400
You can do a call for dbconnection
$actual_dsn = 'your_dns_actual_value'
$yourConnection = new \yii\db\Connection([
'dsn' => $actual_dsn,
'username' => $username,
'password' => $password,
]);
$yourConnection->open();
eventually close the previous open connection
You can do this in your db_login.php depending of the application's needs
Upvotes: 1
Reputation: 1
Maybe :
if (choice =='a') {
'db' => require(__DIR__ . '/db.php')
}
else {
'db2' => require(__DIR__ . '/db_login.php')
}
Upvotes: 0