Codeigniter - 使用两个不同数据库的最佳方式 [英] Codeigniter - best way to use two different database

查看:112
本文介绍了Codeigniter - 使用两个不同数据库的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道在我的应用程序中使用2个不同数据库的最佳做法吗?

Does someone knows the best practice for using 2 different database in my application?

我需要在位于不同位置的两个数据库(主机,

I need to store data in both databases which are differently located (host,username,password , all does change).

我打算像往常一样创建模型,并在构建集中设置db主机,名称,传递等。

I'm planning to create models as usual, and in construct set the db host,name,pass etc.

推荐答案

我不确定你是否称之为最好的方式,但 如教程中所述,这是数据库文件中的

I'm not sure if you call this "best" way, but a way, as described in the tutorial, is this,

,您具有默认配置,其中的一部分是:

in the database file, you have the default configuration, a part of which is:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "database";
$db['default']['database'] = "db1";

现在你可以创建另一个组,我们称之为group1,作为默认数据库设置除了名称,所以你可以做

now you can create another group, say we call it group1 and we want it to have everything the same as the default database settings except for the name, so you can do

$db['group1']=$db['default'];
$db['group1']['database']="db2";

那么,当你想使用第二个数据库时,只要去

then, when you want to use the second database, just go

$DB2 = $this->load->database('group1', TRUE); 

,而不是 $ this-> db-> foo (),您将执行 $ DB2-> foo()

(如sbaaaang的意见中所建议的),可以 $ this-> db = $ DB2; 保持一切一样

alternatively (as suggested in comments by sbaaaang), you can do $this->db=$DB2; to keep everything the same

,您可以将其扩展到多个组,例如

and you can extend this to multiple groups like this

 $DB1 = $this->load->database('group1', TRUE); 
 $DB2 = $this->load->database('group2', TRUE); 
 ...
 $DBn = $this->load->database('groupn', TRUE); 

这篇关于Codeigniter - 使用两个不同数据库的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆