Yii Framework Forum: Set database in module - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Set database in module Rate Topic: -----

#1 User is offline   Johannn 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 12-April 10
  • Location:Netherlands

Posted 03 May 2010 - 07:38 AM

--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?
0

#2 User is offline   Johannn 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 12-April 10
  • Location:Netherlands

Posted 03 May 2010 - 07:59 AM

Hmm think this solved it:

public function getDbConnection()
{
  Yii::app()->dbdemo->active = true;
  return Yii::app()->dbdemo;
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users