Welcome Guest, Not a member yet? Register   Sign In
Using Multiple Database Groups at Once? [SOLVED!!]
#1

[eluser]fancms[/eluser]
I am working on something that uses 2 databases. However, it's pretty tedious having to copy/paste
Code:
$one = $this->load->database('dbone',TRUE);
$two = $this->load->database('dbtwo',TRUE);

in every function (and there's a lot of 'em!). I can't put the two lines in an external function because then I'm unable to use $one->select, $two->query, etc format. Tried putting them in the constructor function, and that doesn't work either.

Is there any shortcut to copy/pasting those two lines hundreds of times?
#2

[eluser]gtech[/eluser]
what happens if you put

$this->one = $this->load->database('dbone',TRUE);
$this->two = $this->load->database('dbtwo',TRUE);

in the constructor

and access

$this->one->

for database calls in the methods inside the class?
#3

[eluser]esra[/eluser]
Try subclassing Controller to create a MY_Controller library and store it in application/libraries/. MY_Controller will be loaded first rather than Controller and will inheirit the methods in Controller. Add the two lines to the MY_Controller constructor, then subclass all of your other controllers from MY_Controller. Those controllers will inheirit the the MY_Controller constructor if their constructors are set to use the parent constructor.
#4

[eluser]fancms[/eluser]
Thanks for the help, gtech and esra. I found that I only needed to put the second (non-default) database line in the constructor class - works for me! Smile

So now I just pop in

Code:
$this->two = $this->load->database('dbtwo',TRUE);

in the constructer which enables me to use

Code:
$this->two->select , $this->two->update

etc throughout the controller

I was actually going to make the Controller subclass as mentioned by esra, but since only 1/2 my app needs the 2nd database I decided to go the other route.

Thanks again! Big Grin
#5

[eluser]esra[/eluser]
Actually, the MY_Controller.php file could contain multiple classes. Thus, various sibling controllers could inheirit from these base controller classes. My_Controller is autoloaded when it exists, making all classes included in the file available to sibling controllers. Then you could have one base controller class to handle multiple database groups and another that does not. The additional base classes included in MY_Controller.php can be subclassed from the MY_Controller class which needs to exist to make the technique work.

However, your solution should work just as well.




Theme © iAndrew 2016 - Forum software by © MyBB