0

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.

Vadym
  • 548
  • 2
  • 8
  • 21

2 Answers2

1

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

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Maybe :

if (choice =='a') {
'db' => require(__DIR__ . '/db.php')
}
else {
'db2' => require(__DIR__ . '/db_login.php')
}