hartanto
hartanto

Reputation: 189

CodeIgniter - Model loaded but can't be used?

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';
    }
}

Upvotes: 1

Views: 1810

Answers (4)

mattumotu
mattumotu

Reputation: 1504

$this->load->model('user_model');

should read

$this->load->model('User_model');

Models names are case-sensitive!

Upvotes: 2

Donny Kurnia
Donny Kurnia

Reputation: 5313

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
    //...
  }

}

Upvotes: 0

hartanto
hartanto

Reputation: 189

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

Upvotes: 0

someoneinomaha
someoneinomaha

Reputation: 1304

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.

Upvotes: 0

Related Questions