• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't load->model after loading a custom class

#1
[eluser]Unknown[/eluser]
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 Wink ), this way:

Code:
$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:

Code:
$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:
- as I said both Admin controller and my custom class ComponiPannello extends CI_Controller
- in ComponiPannello I have this constructor:

Code:
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

#2
[eluser]Harold Villacorte[/eluser]
Libraries should not extend CI_Controller. The super object is an instance of CI_Controller and should only be extended by controllers. Just do like the docs say and reference the super object like this when in a library:
Code:
$CI =& get_instance();


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.