简体   繁体   English

Codeigniter - 如何动态更新数据库配置设置?

[英]Codeigniter - how do I update my database config settings dynamically?

I want to change some database settings for one part of my program. 我想为我的程序的一部分更改一些数据库设置。

In my setup the databse class is autoloaded with a config which looks like this 在我的设置中,数据库类是自动加载的配置,看起来像这样

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

At one part of the script I want to change the value of $db['default']['keyname'] , how can I do this? 在脚本的一部分我想更改$db['default']['keyname'] ,我该怎么做?

There is how class > check it http://codeigniter.com/user_guide/libraries/config.html 有类>检查它http://codeigniter.com/user_guide/libraries/config.html

How ever you can NOT change db connection settings.... They are loaded long before your configs.. 你怎么也不能改变数据库连接设置....它们在你的配置之前很久就被加载了..

You should be adding another set of credentials instead of changing the existing ones dynamically: 您应该添加另一组凭据,而不是动态更改现有凭据:

$db['another_db']['hostname'] = 'localhost';
$db['another_db']['username'] = 'root';
$db['another_db']['password'] = '****';
$db['another_db']['database'] = 'database';
$db['another_db']['dbdriver'] = 'mysql';
$db['another_db']['dbprefix'] = '';
$db['another_db']['pconnect'] = TRUE;
$db['another_db']['db_debug'] = TRUE;
$db['another_db']['cache_on'] = FALSE;
$db['another_db']['cachedir'] = '';
$db['another_db']['char_set'] = 'utf8';
$db['another_db']['dbcollat'] = 'utf8_general_ci';
$db['another_db']['swap_pre'] = '';
$db['another_db']['autoinit'] = TRUE;
$db['another_db']['stricton'] = FALSE;

You can load this other database by doing: 您可以通过执行以下操作来加载此其他数

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

Then use it like normal database driver: 然后像普通数据库驱动程序一样使用

$another_db->select();
...etc

Slightly hacky, but you could add something like this to system/database/DB_driver.php (I've used db password change as an example):- 稍微hacky,但你可以添加这样的东西到system / database / DB_driver.php(我以db密码更改为例): -

public function update_pw($value) {
  $this->password = $value;
}

Then in your project, do 然后在你的项目中,做

$this->db->update_pw('example');
$this->db->reconnect();

Depending on specifically what you want to change in the config - and also more importantly, why you want to change it - this may or may not be the best option. 具体取决于您要在配置中更改的内容 - 更重要的是,您希望更改它的原因 - 这可能是也可能不是最佳选择。

Also you can always use a Global var. 您也可以始终使用Global var。 Normally I always have 2 databases in place. 通常我总是有2个数据库。 One for production and one for development. 一个用于生产,一个用于开发。 You just check that var to load one or the other. 您只需检查var以加载其中一个。

That concept can be used to load one database in one part of your application and other in other part. 该概念可用于在应用程序的一个部分中加载一个数据库,在另一部分中加载其他部分。 However, I would go with the built in solution like Brendan pointed out. 但是,我会像布兰登指出的内置解决方案一样。

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

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