Using two database

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?

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

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:

I got help from this link…

http://www.yiiframework.com/

I override the function getDbConnection() in models to connect to different database…

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.yiiframework.com/forum/index.php/topic/37300-how-to-create-relations-with-multiple-databses-connections/

thanks in advance