在 CodeIgniter 中切换数据库
抱歉,如果这是一个真正的新手问题(也是一个 php 新手),但我无法从文档中弄清楚:
如果我不指定其他任何内容,我希望能够使用默认数据库,并且可以使用可以使用 $this->db 进行以下配置:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'ion_auth';
$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;
$db['visitorsDB']['hostname'] = 'localhost';
$db['visitorsDB']['username'] = 'root';
$db['visitorsDB']['password'] = 'root';
$db['visitorsDB']['database'] = 'visitorsDB';
$db['visitorsDB']['dbdriver'] = 'mysql';
$db['visitorsDB']['dbprefix'] = '';
$db['visitorsDB']['pconnect'] = TRUE;
$db['visitorsDB']['db_debug'] = TRUE;
$db['visitorsDB']['cache_on'] = FALSE;
$db['visitorsDB']['cachedir'] = '';
$db['visitorsDB']['char_set'] = 'utf8';
$db['visitorsDB']['dbcollat'] = 'utf8_general_ci';
$db['visitorsDB']['swap_pre'] = '';
$db['visitorsDB']['autoinit'] = TRUE;
$db['visitorsDB']['stricton'] = FALSE;
但是我想在我的模型之一中使用第二个数据库,所以我尝试了这个:
function __contruct() {
parent::__construct();
$this->db = $this->load->database('visitorsDB', TRUE);
}
public function getAllVisitors($paramArr) {
//$this->db = $this->load->database('visitorsDB', TRUE);
但这不起作用。只有当我取消注释最后一行以便在方法本身中加载数据库时它才会起作用。我不明白为什么。我还尝试在类的开头声明一个新的类变量 - private $myDB; - 然后在构造函数中实例化它 - $myDB = $this->load->database('visitorsDB', TRUE);
但这也不起作用。那么如何切换整个模型的数据库呢?
Sorry if this is a real newbie question (also a php newbie), but I can't figure it out from the docs:
I want to be able to use a default database, if I don't speficy anything else, and that works fine using $this->db with the following configuration:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'ion_auth';
$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;
$db['visitorsDB']['hostname'] = 'localhost';
$db['visitorsDB']['username'] = 'root';
$db['visitorsDB']['password'] = 'root';
$db['visitorsDB']['database'] = 'visitorsDB';
$db['visitorsDB']['dbdriver'] = 'mysql';
$db['visitorsDB']['dbprefix'] = '';
$db['visitorsDB']['pconnect'] = TRUE;
$db['visitorsDB']['db_debug'] = TRUE;
$db['visitorsDB']['cache_on'] = FALSE;
$db['visitorsDB']['cachedir'] = '';
$db['visitorsDB']['char_set'] = 'utf8';
$db['visitorsDB']['dbcollat'] = 'utf8_general_ci';
$db['visitorsDB']['swap_pre'] = '';
$db['visitorsDB']['autoinit'] = TRUE;
$db['visitorsDB']['stricton'] = FALSE;
But then I want to use the second database in one of my models, so I tried this:
function __contruct() {
parent::__construct();
$this->db = $this->load->database('visitorsDB', TRUE);
}
public function getAllVisitors($paramArr) {
//$this->db = $this->load->database('visitorsDB', TRUE);
But that doesn't work. Only if I uncomment the last line so that I load the database in the method itself will it work. I don't understand why. I also tried declaring a new class variable at the beginning of the class - private $myDB; - and then instantiating it in the constructor - $myDB = $this->load->database('visitorsDB', TRUE);
But that didn't work either. So how can I switch database for the entire model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当数据库的登录用户更改时
when login user with database changed
我不建议通过覆盖
$this->db
来选择第二个数据库。只需在您需要的模型中为您的其他数据库添加一个属性并通过它访问它即可。在你的模型中这样做:我知道在你的问题中你说过你做了类似的事情,但我不确定你是否做得正确。这将是这样做的正确方法,我不明白为什么如果这样做就行不通。
I wouldn't recommend selecting a second database by overwriting
$this->db
. Just add a property for your other database in the model you need it and access it through that. In your model do this:I know in your question you said that you did something similar but I'm not sure if you did it correctly. This would be the correct way of doing so and I don't see why it wouldn't work if it was done this way.