CodeIgniter Forums

Full Version: Loading DB Class in Helpers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]pmonty[/eluser]
I am sure this is as obvious as the nose on my face, but how do I implement loading the DB class in a helper function?

Paul

El Forum

[eluser]Référencement Google[/eluser]
$CI =& get_instance();
$CI->load->model('your_model');

El Forum

[eluser]tonanbarbarian[/eluser]
Utilizing CodeIgniter Resources within Your Library
works for helpers and plugins as well

El Forum

[eluser]pmonty[/eluser]
Thanks, all. That is exactly what I was looking for.

Paul

El Forum

[eluser]pmonty[/eluser]
Well, trying to implement this, I am still having some problems. Here is my code.

Code:
CI =& get_instance();
$params = array('default', TRUE);
$DB1 = $CI->load->library('database',$params);

Can anyone see why this returns the PHP Error
Quote:An Error Was Encountered
Unable to load the requested class: database

Paul

El Forum

[eluser]xwero[/eluser]
no dollar sign on the first line Smile

El Forum

[eluser]pmonty[/eluser]
Just a typo in the post. It is there in my code but generating the error as noted.

Corrected and as in my helper code:

Code:
$CI =& get_instance();
$params = array('default', TRUE);
$DB1 = $CI->load->library('database',$params);

Paul

El Forum

[eluser]xwero[/eluser]
The database class isn't loaded as a library, it has its own load method
Code:
$CI->load->database(); // loads default group
$CI->db->query(); // example use methods first loading possibility
$DB1 = $CI->load->database('default',true); // loads default group as an independent object
$DB1->query(); // example use methods second loading possibility

I think you mixed up a few things in your snippet

El Forum

[eluser]pmonty[/eluser]
Yup, Thanks Xwero.

I was confusing sample language from the docs with how to actually implement the call. Your examples did it.

Thanks again.

paul