Welcome Guest, Not a member yet? Register   Sign In
Second database in libraries
#1

[eluser]DougW[/eluser]
I need my CI app to access two different databases. I do this by defining the second one in config and then in the models where I want to use it, I do the following:

$this->otherdb = $this->load->database('other', TRUE);

in the constructor for the model.

My problem now is I have a class file (library) that I need to access this second database in. I tried putting the above line int he controller, then doing this:

$CI =& get_instance();
$this->otherdb = & $CI->otherdb;

In the constructor of the class, (of course defining var otherdb=NULL as well) but it doesn't seem to work. I know it kinda goes against the philosophy of models to do database activity in the library, but they are ajax functions and I am trying to keep them together. Is there a way to give access to or load databases in the library classes or is there an easy way to load a model from withing the class? What is the 'best practice' method?
#2

[eluser]Colin Williams[/eluser]
You need to do the get_instance() in the model where you load the database, too. Because you are assigning that database to a model property, and you want to assign it to a property of the controller class. OOP 101 Smile
#3

[eluser]DougW[/eluser]
I did that but that isn't the problem. I don't want to load a model. I want to load the database in the library class.

I did it this way and it works now...

$CI =& get_instance()
$CI->load->database(‘other’, TRUE);

As for OOP 101, I missed that. I learned to program back when we used assemblers and compilers. Smile

Thanks for the help! As usual, right after I asked the question I figured it out.
#4

[eluser]Colin Williams[/eluser]
Best of luck to you..




Theme © iAndrew 2016 - Forum software by © MyBB