简体   繁体   English

使用两个数据库Yii 1

[英]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 : 在yii Wiki上有一些很好的文章涵盖了大部分内容:

If you don't quiet get it, do read the comments in that article, some good stuff there. 如果您不喜欢它,请阅读该文章中的评论,那里有一些不错的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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