开发者

Trouble changing databases for my models in codeigniter

I'm making a website with two different databases. Let's say one is DB1, and the other is DB2. I've set up my database.php in the config folder, so they each have the correct host/password/username/database etc with db['DB1']['hostname'] and the other db['DB2']['hostname'] etc, so I'm pretty sure I've got that part right.

According to CodeIgniter docs, I'm supposed to do $this->load->database('DB1') to get that o开发者_JAVA百科ne to work within my model. First, I put it in my constructor for my model, and it didn't work. Then I tried putting that line as the first line of every function in the model, and that didn't work. I'm still getting this error:

Error Number: 1146

Table 'DB2.stores' doesn't exist

When I'm trying to use DB1, and have done $this->load->database('DB1') as the first line of the model's function. Does anyone know what I'm doing wrong? Thanks!


There are a couple of caveats I ran into when I last tried to do that.

In your model constructor, load DB1 and DB2 as

$this->DB1=$this->load->database('DB1',true) //<-- notice the true parameter
$this->DB2=$this->load->database('DB2',true) //<-- notice the true parameter

and then use $this->DB1->get()->where()->result_array() etc.etc.

Caveat 2
make sure persistent connections are off... they are on by default in CI

$db['DB2']['pconnect'] = FALSE;

and

$db['DB2']['pconnect'] = FALSE;

in your application/config/database.php file

hope that helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜