Reputation: 118
I’m learning CI coming from a non MVC OOP in-house framework, so I’m still confused on a bunch of things, I don’t always understand what is normal OO programming VS. things that done the code igniter way. Anyway I’ll try to explain my problem:
I have one controller, called Admin which extends CI_Controller.
I made a class (let’s say custom class meaning is something I made not part od CI) I put that class in the library folder, that class extends CI_Controller as well (that extension allowed me to have load method and some other CI methods).
From my Admin controller, I’m loading my custom class (named “ComponiPannello” since I’m italian ), this way:
$parameters = array('baseUrl' => $this->baseUrl);
$this->load->library('ComponiPannello', $parameters);
everything works perfectly my “ComponiPannello” does hits job and returns something I need for my Admin view.
Now I wanted to load a model right after that class, because I need to pass more info to the Admin view, those info are stored in the DB, I have a specific model to pull those extra info, so I added a few lines and the code looks as follow:
$parameters = array('baseUrl' => $this->baseUrl);
$this->load->library('ComponiPannello', $parameters);
$setup = $this->componipannello->return_data();
$this->data = array_merge($this->data, $setup);
$this->load->model('categories_model');
$this->data['categories'] = $this->categories_model->get_categories();
were $this->data is what I’m passing to the Admin’s view.
Few more info:
in ComponiPannello I have this constructor:
function _construct($params) { parent::_construct(); $this->load->library('session'); $this->baseUrl = $params['baseUrl']; }
if I move the $this->load->model part before the $this->load->library I get no errors everything works and returns the correct data, but I would like to learn what I’m doing.
if I keep it as I have it the error I get is the following:
Fatal error: Call to a member function get_categories() on a non-object in ... on line ...
I hope I was able to explain my issue in a way that is clear enough for you to help me, thanks in advance for the time spent.
Luke
Upvotes: 0
Views: 2560
Reputation: 5248
Whay u use database object in ur library?
Utilizing CodeIgniter Resources within Your Library. To access CodeIgniter's native resources within your library use the get_instance() function. This function returns the CodeIgniter super object.
Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct:
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
etc.
$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:
First, assign the CodeIgniter object to a variable:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.
Upvotes: 3
Reputation: 1576
Your problem is probably coming about because you are loading another controller as a library, when in fact, it is not a library.
You can load libraries, helpers, etc from a library like this:
$CI =& get_instance();
From within your library, instead of $this->load->helper()
to load a helper you would now use $CI->load->helper()
.
More info on this is available in the CI documentation: http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
Upvotes: 4