简体   繁体   中英

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM