我使用三台数据库服务器和一台应用程序服务器。
class Main extends REST_Controller{
public function __construct(){
parent::__construct();
if($this->input->get('country', true) == "KR")
{
$krdb['host'] = '11.22.33.44';
//blah blah
$this->load->model('category', '', $krdb);
$this->load->model('contents', '', $krdb);
$this->load->model('user', '', $krdb);
$this->load->model('series', '', $krdb);
}
else if($this->input->get('country', true) == "EN")
{
$endb['host'] = '111.222.33.44';
//blah blah
$this->load->model('category', '', $endb);
$this->load->model('contents', '', $endb);
$this->load->model('user', '', $endb);
$this->load->model('series', '', $endb);
}
}
}
但是这次运行,无法加载数据库。
如果改变条件 if(1==1),右激活。
我的猜测,在获取参数设置之前调用Controller构造函数
请帮帮我..
最佳答案
在您的 config/database.php
中,您的数据库被配置为 'default'
组,如下所示:
<?php
$db['default']['hostname'] = 'host';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database';
This allows you to specify other groups like this:
$db['KR']['hostname'] = 'host';
$db['KR']['username'] = 'username';
$db['KR']['password'] = 'password';
$db['KR']['database'] = 'database';
$db['EN']['hostname'] = 'host';
$db['EN']['username'] = 'username';
$db['EN']['password'] = 'password';
$db['EN']['database'] = 'database';
You can then connect to another database by using:
输入 Controller
class Main extends REST_Controller {
public function __construct() {
parent::__construct();
if ($this->input->get('country', true) == "KR") {
$defaultDB = $this->load->database('KR', TRUE);
} else if ($this->input->get('country', true) == "EN") {
$defaultDB = $this->load->database('EN', TRUE);
}else{
$defaultDB = $this->load->database('default', TRUE);
}
}
}
关于php - codeigniter 负载模型使用获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30390042/