nowiko
nowiko

Reputation: 2557

Work with two databases Yii 1

I has two databases. I want to get some values from first and save them to second, but I dont want to create model for tables in second database, if I use code like this will be ok ?

$user = Yii::app()->db->createCommand()
->select('username, password')
->from('tbl_user')
->where('id=:id', array(':id'=>1))
->queryRow();

Upvotes: 0

Views: 1073

Answers (1)

DarkMukke
DarkMukke

Reputation: 2489

You can define as many databases if you want in your app

'components' => array(
    'db' => array(
        'connectionString' => 'mysql:host=dbserver1;dbname=my1db',
        ...
    ),
    'otherdb' => array(
        'connectionString' => 'mysql:host=dbserver2;dbname=my1db2',
        ...
    ),

and then you can use this as

$user = Yii::app()->otherdb->createCommand()
    ->select('username, password')
    ->from('tbl_user')
    ->where('id=:id', array(':id'=>1))
    ->queryRow();

There is a few good articles covering most of this on the yii wiki :

If you don't quiet get it, do read the comments in that article, some good stuff there.

Upvotes: 2

Related Questions