CodeIgniter Forums

Full Version: Using multiple databases with helper files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ryan656[/eluser]
I am writing an application that's using separate databases for scalability and I'm running into problems. I have helper files that interact with the databases and I use these helper files in multiple controllers. Here's an example of my problem.

I have a login controller that loads a 'user' database and calls a 'checkLogin' function contained in a helper file. It works fine when using one database, I load the database in the login controller with $this->load->database('dbusers') and in the helper file I can get the CI instance with $ci = &get;_instance() and then perform the query with $user = $ci->db->query("select statement").

The problem is when I try to use more than one database, the user guide says that to run multiple databases you write this:

$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

But when I try to do this I cannot get the code in the helper file to work cause it keeps coming back with an 'undefined' error.

This is what I tried to do: $dbusers = $this->load->database('dbusers', TRUE) in the login controller and then something like this in the helper file:

$ci = &get;_instance()
$user = $ci->dbusers->query("select statement")

I've also tried $user = $dbusers->query("select statement") but nothing seems to work.


I hope this makes sense and I know there's a way to do this in Codeigniter I just can't figure it out. Could the problem be I'm not using any models or that I'm loading the database in the controller and using it in the helper?

Any help would be greatly appreciated.


Thanks,
Ryan

El Forum

[eluser]ryan656[/eluser]
I just read my post and it may be a bit confusing, basically I am looking for a way to access multiple databases (loaded in a controller) from a helper file.


If this is how you load multiple databases:

$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

And this is how you access the CI instance in the helper file:

$ci =& get_instance();

How do you use the CI instance to access the DB's loaded in the controller?

Again, I've tried:

$ci->db1->query(); and $db1->query() - neither of which seem to work.


Thanks again,
Ryan

El Forum

[eluser]JoostV[/eluser]
Helpers are not classes, but merely functions containers. They do not have native access to the CI superobject.

You should do you data handling in the model. In models, you can reference the CI superobject as $this. Therefore, this should work:
Code:
$db1 = $this->load->database('group_one', TRUE);
$query = $db1->get();
if ($query->num_rows() > 0) {
   // Strut your stuff
}

If you need to use the CI superobject in a helper sometime anyway you have to pass it as a parameter.
Code:
echo some_helper_function($this);

El Forum

[eluser]ryan656[/eluser]
Thanks for the reply,

I can access the CI superobject from the helper with <code>$ci =& get_instance();</code> so is there a way to access both databases with the superobject or is the only solution to use a model?

Thanks again,
Ryan

El Forum

[eluser]ryan656[/eluser]
I'm fairly new to codeigniter so sorry if I'm being kinda lame Smile

El Forum

[eluser]JoostV[/eluser]
Sorry, my bad. You CAN access the superobject from a helper. I just never do Smile
OK, then this will work. In your helper (just tested it):
Code:
function some_function ()
{
    $ci = & get_instance();
    $db1 = $ci->load->database('group_one', TRUE);
    $query = $db1->get('some_table');
    if ($query->num_rows() > 0) {
        return $query->result_array();
    }
}

[EDIT] Having said that, it's good MVC practise to do all data handling in your models. http://ellislab.com/codeigniter/user-gui...w/mvc.html

El Forum

[eluser]ryan656[/eluser]
Ok perfect thanks - now is there a way to call <code>$db1 = $ci->load->database('group_one', TRUE);</code> from the controller and then <code>$query = $db1->get('some_table');</code> from the helper? I just don't wanna load the database every time the helper function is called. Maybe it doesn't matter though I don't know, does the load function check to see if its already loaded before it wastes resources trying to load it?

Thanks again I appreciate the help.

Ryan

El Forum

[eluser]JoostV[/eluser]
Once again, it is very bad practise to put data handling in your helpers. Google why.

But, as a theoretical discussion, you simply pass the $db object as a parameter.
controller:
Code:
$db1 = $this->load->database('group_one', TRUE);
$result = some_function($db1);

helper:
Code:
function some_function($db){
    $query = $db->get('some_table');
    return $query->num_rows() > 0 ? $query->result_array() : array();
}

El Forum

[eluser]ryan656[/eluser]
OK Thanks! - I'm working on making a model right now, hopefully I can get it to work.

Thanks a bunch for your help.

Ryan

El Forum

[eluser]ryan656[/eluser]
Maybe I'm missing the point of helpers and models - why is it bad to have some helper functions that query the db? For instance, one of the functions in my login helper takes the username and password and checks the db for the user, should this function be in a model or should just the query call be in a model or what? The function also sets session information if the user is found, does this belong in the model as well?

I guess im asking..does every function that performs CRUD on a database belong in a model? And is it the whole function or just the part that performs the query?

Thanks,
Ryan