简体   繁体   English

在 Codeigniter Mongo 类中切换数据库

[英]Switch DB in Codeigniter Mongo class

I am using codeigniter mongo class.我正在使用 codeigniter mongo 类。 I store the config information like mongohost, mongodb name etc in config/mongo.php in codeigniter.我将 mongohost、mongodb 名称等配置信息存储在 codeigniter 的 config/mongo.php 中。 However, I would like to switch the db name in some places in my code, without having to re-write the config/mongo.php each time.但是,我想在我的代码中的某些地方切换数据库名称,而不必每次都重新编写 config/mongo.php。 Is there a switch db function or any other way?是否有 switch db 函数或任何其他方式?

You can set another in the config aside from your default one : 除了默认设置之外,您还可以在配置中设置另一个

// your code
$this->load->database('test');

// config
$db['test']['hostname'] = "localhost";
$db['test']['username'] = "root";
$db['test']['password'] = "";
$db['test']['database'] = "database_name";
$db['test']['dbdriver'] = "mysql";
$db['test']['dbprefix'] = "";
$db['test']['pconnect'] = TRUE;
$db['test']['db_debug'] = FALSE;
$db['test']['cache_on'] = FALSE;
$db['test']['cachedir'] = "";
$db['test']['char_set'] = "utf8";
$db['test']['dbcollat'] = "utf8_general_ci";
$db['test']['swap_pre'] = "";
$db['test']['autoinit'] = TRUE;
$db['test']['stricton'] = FALSE;
$this->mongo->switch_db("data base name");

这应该会成功切换数据库。

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

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