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
-
When is getDbConnection called?
-
How to set the db for each instance of a model?
-
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…