Set database in module

–Not sure if I can adjust the topic title, it’s model not module…

I’ve got a model named banners, all other models make use of the main database. But this one has to use a different database. In de main.php right underneath the other db connection I added a second one.

If I do stuff like this in a controller:




$connection=Yii::app()->dbdemo;

$connection->active=true;

$sql = "Select * From Banners"; 

$banners = $connection->createCommand($sql)->queryAll();

$connection->active=false;



It works. What I want to do now is once I’ve submitted a form make it choose the proper database.

so in my model I have the following function:




public function getDbConnection()

{

return Yii::app()->dbdemo;

}



but now I get the error: CDbConnection is inactive and cannot perform any DB operations.

Did I forget to activate it somewhere, if yes where?

Or do I have to do it in a completely different way?

Hmm think this solved it:




public function getDbConnection()

{

  Yii::app()->dbdemo->active = true;

  return Yii::app()->dbdemo;

}