Hi guys,
I have a main database (called dbmain) containing records of my customers.
Each customer has a customer number (1, 2, 3,) and his own database (dbtenant1, dbtenant2, dbtenant3,).
In the config file, I have two database components called db and db2 and both are connected to dbmain.
db -> dbmain
db2 -> dbmain
After a user has login, a behaviour changes the connection of db2 to the user’s branch’s dbtenant.
db -> dbmain
db2 -> dbtenant3
This all works nicely.
But when a user forgets/resets his password, I want to make him inactive in all dbtenant databases, to force him to regain access via his branch’s User Manager (for security reasons).
So I get an array of the existing branches, and simply loop through them, using their branch_id to connect to each branch’s own database.
(At this stage the user is not login - so db2 is still connected to dbmain.)
foreach($branchesToUpdate as $branchToUpdate)
{
/* Create a new connection. */
$database = 'dbtenant' . $branchToUpdate->branch_id;
$dsn = 'mysql:host=' . $branchToUpdate->tenant_db_host . ';dbname='.$database;
$dbUsername = Yii::app()->db2->username;
$dbPassword = Yii::app()->db2->password;
/* Override existing db2 component */
try{
$component = Yii::createComponent(array(
'class'=>'CDbConnection',
'tablePrefix' => 'cap_',
'connectionString' => $dsn,
'emulatePrepare' => true,
'username' => $dbUsername,
'password' => $dbPassword,
'charset' => 'utf8',
'enableParamLogging'=>true,
));
Yii::app()->db2->setActive(FALSE);
Yii::app()->setComponent('db2',$component);
Yii::app()->db2->setActive(TRUE);
}
catch (Exception $e)
{
throw new Exception();
}
/* All okay. Make the user inactive here - in the current db2 */
}
The code is working and there are no exceptions. The user is made inactive in dbtenant1. (Remember that db2 was first connected to dbmain - so it is impossible to make the user inactive in dbtenant1 if the above code was not working.)
The problem is that the user is only made inactive in dbtenant1 and not in the subsequent dbtenant databases.
Debugging showed that $component is generated correctly; changing from dbtenant1 to dbtenant2 etc. with each loop.
But it is almost as if the following is working only once, because db2 stays connected to dbtenant1.
Yii::app()->db2->setActive(FALSE);
Yii::app()->setComponent(‘db2’,$component);
Yii::app()->db2->setActive(TRUE);
Any ideas?