Help needed How to use a different Db for each new instance of AR

Anyone please help…

I need write a AR model to write to different db (because each db belong to a different client) for each instance. I try overriding AR getDbConnection but does not seem to work.

Question

  1. When is getDbConnection called?

  2. How to set the db for each instance of a model?

  3. When I call ar_object::model(), should not did not call for init()?

Below are some of my codes with respective to this,

class Site extends CActiveRecord

{

public $useAccount;

public function init()

{

if(empty($this->useAccount)) $this->useAccount = Yii::app()->params[‘useAccount’];

}

public function getDbConnection()

{

if(!empty($this->useAccount))

{

}

elseif(!empty(Yii::app()->params[‘useAccount’]))

{

$this->useAccount = Yii::app()->params[‘useAccount’];

}

else

{

$this->redirect(array(’/’.$this->id.’/message/db-not-selected.’));

}

return Database::getDbConnection(Database::ACCOUNT, $this->useAccount);

}

in controller…

public function actionManage()

{

Yii::app()->params[‘useAccount’] = ‘database_a’;

$modelA = new Site;

Yii::app()->params[‘useAccount’] = 'database_b;

$modelB = new Site;

Does not seem to work.

Anyone please enlightenment me, please…

Hello, i think u have a table(A) that folds a list of all ur clients then have another table(-B ) that will hold all the other details, which will have a foreign-key that will refer to the primary of table(A).

i think u need to re-model ur db table structure.

another option is please read this click Here

It is not about Table I am referring to but rather database. Is it possible to connect to different database for different AR instance of the same model?

There are a few posts about this on the forum… one is here - http://www.yiiframew…dpost__p__45760

So basically the getDbConnection() return the database component that will be used for the query… you will need a way to tell it what database you need…