CodeIgniter Forums
[SOLVED] How do you connect to two databases cleanly ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [SOLVED] How do you connect to two databases cleanly ? (/thread-2285.html)



[SOLVED] How do you connect to two databases cleanly ? - El Forum - 07-27-2007

[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.


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 07-29-2007

[eluser]Danilo Stern-Sapad[/eluser]
See: http://ellislab.com/codeigniter/user-guide/database/connecting.html

It explains how this can be done pretty well.


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 07-30-2007

[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?


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 08-01-2007

[eluser]Morty[/eluser]
[quote author="Danilo Stern-Sapad" date="1185771443"]See: http://ellislab.com/codeigniter/user-guide/database/connecting.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.


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 08-01-2007

[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.


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 08-01-2007

[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!


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 08-01-2007

[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...


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 08-01-2007

[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/


[SOLVED] How do you connect to two databases cleanly ? - El Forum - 08-01-2007

[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