Hi there!
I long story short…
I’ve read few post:
tip: multiple db connection solution
I need to be able to connect to 2 DB according to the constructor.
[color="#0000FF"]Connection to DB1: [/color]
MyModel::model()->findAllByAttributes(array('id'=>$ID);
[color="#0000FF"]Connection to DB2:[/color]
$myModel = new MyModel( null, $tenant_id ) //This one should connect to the DB of my client {tenant_id}.
$myModel->findAllByAttributes(array('id'=>$ID);
[color="#0000FF"]I use 2 componants( DynamicDB, extDynamicDB ) to create 2 singleton CDbConnection[/color]
class TENANT_Page extends CActiveRecord
{
public $tenantID;
public $externalTenantDB;
public function __construct( $scenario = 'insert', $tenantID = null )
{
if ( isset($tenantID) )
{
$this->tenantID = $tenantID;
}
parent::__construct($scenario);
}
public function getDbConnection()
{
if ( !isset($this->tenantID) )
{
return Yii::app()->DynamicDB->getTenantConnection();
} else {
$this->externalTenantDB = Yii::app()->extDynamicDB->getTenantConnection($this->tenantID);
// parent::$db = $this->externalTenantDB;
return $this->externalTenantDB;
}
}
[...]
[color="#0000FF"]Before using this tenant, I need to verify its existence and fetch it’s connection info.[/color]
class DynamicDB extends CApplicationComponent
{
public $connection;
public $externalTenant;
public function getDbConnection( $IPaddress, $dbName, $username, $password )
{
if( $this->connection != null)
$this->connection->active=false;
$this->connection=new CDbConnection('mysql:host='.$IPaddress.';dbname='.$dbName, $username, $password);
$this->connection->active=true;
return $this->connection;
}
public function getTenantConnection( $tenantID = null )
{
if ( !isset($tenantID))
{
return Yii::app()->tenant->db;
} else {
/*
$sql = "SELECT ID FROM GTBtenant WHERE 'ID'='$tenantID ' ";
//$this->externalTenant = GTBtenant::model()->findByPk($tenantID);
$this->externalTenant = new GTBtenant();
// THESE ARE 3 WAYS TO CREATE THE SAME DB CONNECTION!
//$connection= $this->getDbConnection('host.com','db1','user','password');
$connection = new CDbConnection( 'mysql:host=host.com;dbname=db1', 'user', 'pass' );
// $connection = Yii::app()->db;
$connection->active = true;
$command = $connection->createCommand($sql);
$dataReader = $command->query();
$connection->active = false;
foreach ( $dataReader as $row )
{
$this->externalTenant->IPaddress = $row['IPaddress'];
$this->externalTenant->DBname = $row['DBname'];
$this->externalTenant->username = $row['username'];
$this->externalTenant->password = $row['password'];
}
*/
// THESE ARE RAW INFO FOR TESTING PURPOSES AND IT STILL DOENS'T WORK!!
$this->externalTenant = new GTBtenant();
$this->externalTenant->IPaddress = 'host.com';
$this->externalTenant->DBname = 'db1';
$this->externalTenant->username = 'user';
$this->externalTenant->password = 'pass';
if ( !isset($this->externalTenant) )
{
Yii::app()->alert->sendAlert( Alert::ERROR_ALERT, 'the tenant ('. $tenantID .') couldnt be found, when requested by class ['.__CLASS__.']' );
} else {
return $this->getDbConnection( $this->externalTenant->IPaddress,
$this->externalTenant->DBname,
$this->externalTenant->username,
$this->externalTenant->password );
}
}
}
}
[color="#FF0000"]This is where all hell breaks loose.[/color]
When I go fetch info from the SAAS DB, the connection gets overriden! The function search into the SAAS DB info from the TENANT DB so I can’t save() anything
1- Does making 2 seperate componants for the DB connection a must?
2- How can I keep my second connection to be active?
Thank you again for your time