Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] How do you connect to two databases cleanly ?
#1

[eluser]Morty[/eluser]
I am currently using CI with one, sometimes two databases.
However, I don't know how to call them properly.

ATM, I have one load call for each of my models' search. It works but I find it unsatisfying.

Where are we supposed to start the connection? is it in the model (logical) or in the controller?

I'd be interested to know how you create your connections for multiple databases.
#2

[eluser]Danilo Stern-Sapad[/eluser]
See: http://ellislab.com/codeigniter/user-gui...cting.html

It explains how this can be done pretty well.
#3

[eluser]BravoAlpha[/eluser]
[quote author="Morty" date="1185551477"]here are we supposed to start the connection? is it in the model (logical) or in the controller?[/quote]
What happens if you put the connection inside each model, but then you load multiple models from one controller?
#4

[eluser]Morty[/eluser]
[quote author="Danilo Stern-Sapad" date="1185771443"]See: http://ellislab.com/codeigniter/user-gui...cting.html

It explains how this can be done pretty well.[/quote]

Not really. It tells you that you can load your connections, and/or you can set up an active one to be autoloaded. It does not help you to choose where to load it, and in which conditions.

I already browsed all database documentation on CI and there was not a clear answer.

Thanks though.
#5

[eluser]Morty[/eluser]
[quote author="BravoAlpha" date="1185800835"][quote author="Morty" date="1185551477"]here are we supposed to start the connection? is it in the model (logical) or in the controller?[/quote]
What happens if you put the connection inside each model, but then you load multiple models from one controller?[/quote]

You load the same connection multiple times. And I do think that you are creating multiple connections but I may have misunderstood. At least that is what my log says.
#6

[eluser]Henrik Pejer[/eluser]
You would, of course, have to save the different connections.

Something like this should work:

Code:
$this->db1=$this->load->database('group_one', TRUE);
$this->db2=$this->load->database('group_two', TRUE);

then you'd use $this->db1->query('select...') or $this->db2->query('select....') whenever you want to use the connection.

I haven't used two databases with CI but I believe that should work.

Good luck!
#7

[eluser]Morty[/eluser]
It is precisely what I was using. But it made my logfile spam connection to databases so I suppose that I did something wrong...
#8

[eluser]Henrik Pejer[/eluser]
Search the forums and see if you can find anything.

I've found these which are somewhat related:

http://ellislab.com/forums/viewthread/45769/

http://ellislab.com/forums/viewthread/50169/
#9

[eluser]Morty[/eluser]
I did not find them but I understand why. I was looking for two instead of "multiples".

Thanks, it seems to be my solution !

=>SOLVED




Theme © iAndrew 2016 - Forum software by © MyBB