Yii Framework Forum: Using two database - Yii Framework Forum

Jump to content

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

Using two database How can we use different connection to two different database? Rate Topic: -----

#1 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 13-March 12

Posted 14 March 2012 - 10:10 AM

Hi,

I am using two models 1)User::model() to update read and delete user information and 2)POST::model() to get the blog post..

USER MYSQL is already in protected/data/.. I need to copy POST but the schema and db name are same..
How do we change such that POST uses Yii::app db1 connect to connect to the data and USER uses the db2 connection to the data?
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 14 March 2012 - 03:03 PM

in your config/main.php file you should define both databases such as:

'db1'=>array(
                    'class'=>'CDbConnection',
                    'connectionString'=>'mysql:host=localhost;dbname=somename',
                    'username'=>'someusername',
                    'password'=>'password',
                    'charset'=>'utf8',
                    'emulatePrepare' => true,
                ),


'db2'=>array(
                    'class'=>'CDbConnection',
                    'connectionString'=>'mysql:host=localhost;dbname=somename',
                    'username'=>'someusername',
                    'password'=>'password',
                    'charset'=>'utf8',
                    'emulatePrepare' => true,
                ),


and then you will be able to call
Yii::app()->db1->
and
Yii::app()->db2->

1

#3 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 13-March 12

Posted 15 March 2012 - 01:35 AM

Hi bettor, could I use db1 and db2 having different database connection..

'db1'=>array(
                    'class'=>'CDbConnection',
                    'connectionString'=>'mysql:host=localhost;dbname=dabtabase1',
                    'username'=>'someusername',
                    'password'=>'password',
                    'charset'=>'utf8',
                    'emulatePrepare' => true,
                ),


'db2'=>array(
                    'class'=>'CDbConnection',
                    'connectionString'=>'mysql:host=localhost;dbname=dabtabase2',
                    'username'=>'someusername',
                    'password'=>'password',
                    'charset'=>'utf8',
                    'emulatePrepare' => true,
                ),



In model User and POST where do we call
Yii::app()->db1->
and
Yii::app()->db2->

.. it is not using MYSQL instead it is using sqllite..

Thanks and Regards
B L Praveen :mellow:
0

#4 User is offline   B L Praveen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 13-March 12

Posted 16 March 2012 - 12:37 AM

I got help from this link..
http://www.yiiframework.com/...
I override the function getDbConnection() in models to connect to different database..
0

#5 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 21 March 2013 - 07:48 AM

But how to play with relations :)

For example if I had users table in database

user_id - usernmae - password .... etc .

I need to create relations with posts table in another database

post_id - user_id - visitors ...etc

is it possible ? if yes how to call it ?

This my full qustion can any one help ?
http://www.yiiframew...es-connections/

thanks in advance
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