Welcome Guest, Not a member yet? Register   Sign In
Switching Databases
#1

[eluser]NotoriousWebmaster[/eluser]
I'm writing a utility app, which will allow me to perform tasks on different instances of the DB, based on the user's selection of a radio button on the form.

I figured I could force the selection of the production vs dev DB when loading the model; but regardless of the array I pass, it always seems to go with the localhost DB. Sad

Here's a question: if the DB is selected in the controller's constructor, will the model load function override this?

Also, I should mention I'm using an older version of CI: 1.7.2. Would this affect it's behaviour?

Thanks for your help.

- AAA
#2

[eluser]danmontgomery[/eluser]
CI won't automagically overwrite anything once it's already been assigned. If you load the DB normally, then try to load it again, nothing will happen. What you can do is explicitly assign it to $this->db (or any other variable).

Code:
$this->load->database(); // $this->db is db1
$this->load->database('db2'); // $this->db is still db1, db2 is not loaded since $this->db exists
$this->db = $this->load->database('db2', TRUE); // $this->db is db2

This is covered in the user guide ("Connecting to Multiple Databases")
#3

[eluser]NotoriousWebmaster[/eluser]
Thank you noctrum! Especially for the link to the docs. I hadn't absorbed that first time through, I guess.




Theme © iAndrew 2016 - Forum software by © MyBB