views:

236

answers:

3

Ok. So I am working on a website using CI. Here is the structure of my controller:

class MY_Controller extends Controller class User extends MY_Controller class User_Model

So, I load the User_Model inside the constructor of the User controller. I know that it is loaded properly because I tried to print something from User_Model and it worked just fine. However, when I use one of the functions in the User_Model from User controller, it started giving me the error. This is the error I received:

"Undefined property: User::$User_Model"

Anybody has any idea?

This is extended controller

class MY_Controller extends Controller {
    public function __construct() {
      parent::Controller();
    }
}

This is the controller

class User extends MY_Controller {
    public function __construct() {
      parent::__construct();
      $this->load->model('user_model');
      echo $this->user_model->validate_user('hartantothio');
    }
}

This is the User_model

class User_model extends Model {
    public function __construct() {
        parent::Model();
    }        
    public function validate_user($user, $pass = '') {
        return '123';
    }
}
A: 

Are you calling the function using the syntax, $this->User_Model->function_name()?

I also know that I've run into problems with case sensitivity as well in the past.

someoneinomaha
Yes.. This is what I do:$this->load->model('User_Model');echo $this->User_Model->validate_user($data['username']);Giving me error on the second line.
squall15
should it be: $this->User_model->validate_user($data['username']);
someoneinomaha
A: 

Well, by taking the MY_Controller and extends the User controller directly of the Controller, that fixes the issue.

squall15
A: 

Where do you put My_Controller file? I put mine in system/application/libraries and don't have any problems with it. Also, I use PHP4 constructor way to write it, instead of __constructor:

class MY_Controller extends Controller {

  var $is_ajax_request = '';
  var $is_ajax_form = '';

  function MY_Controller()
  {
    parent::Controller();
    //initialize
    $this->is_ajax_request = ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest');
    $this->is_ajax_form = ($this->input->post('ajax') == 'ajax');
    log_message('debug', "MY_Controller Class Initialized");
    //do extra stuffs here
    //...
  }

}
Donny Kurnia
Yes. I put it in that folder as well. I have also used the old style constructor (same name as the class) and it still doesn't work. Hah... This is weird.
squall15
Uhm, I never put load code in constructor. I always do load model and call model's method in controller's method, such as `function index()`. My Controller's constructor is usually just call parent constructor.
Donny Kurnia