CodeIgniter: Undefined property when calling the model

1.6k Views Asked by At

I use CodeIgniter 3 with HMVC.

Does anyone know why do I get the following error

    Severity: Notice
    Message: Undefined property: Login::$login_model

On the second line below. I clearly call the model, but for some odd reason it is not linked properly

    $this->load->model('auth/login_model');
    $response = $this->login_model->attempt($username, sha1($password));

Then the model is pretty basic :

    <?php
    class Login_model extends CI_Model 
    {
        public function attempt($user, $pass) 
        {
        ... 

Now if I use an object it works, but I have the same issue in many places, including the place where I have

    $this->db->select("....

where is crashing as there is no "db". What is the new solution for CodeIgniter 3? I've seen older posts, but none seem to help me out

Thanks!

2

There are 2 best solutions below

2
Jaydev Vara On

just try this code put in controller:

public function __construct() {

    parent::__construct();


    $this->load->model('Login_model'); // load model 


}
0
Marius On

Problem is resolved, the issues were caused by the fact that my controller extended CI_Controller instead of MX_Controller. So changing

       class Login extends CI_Controller 

to

       class Login extends MX_Controller 

resolved the issue.

It took me a while to figure it out by debugging the thirdparty/MX/Loader.php, but once I saw it was looking for MX_Controller type I did the change and it worked perfectly.

This issue is one in many related to migration from CI 2 to CI 3 and also using the HMVC from Wiredesignz. Another big one is uppercase of file names and uppercase on the calls, so strictly referring to this issue I had to uppercase the calls in my controller (changed "login" to "Login"):

     $this->load->model('auth/Login_model');
     $response = $this->Login_model->attempt($username, sha1($password));

I did the above change already, so this was no longer a blocker, still I wanted to put it here just in case someone hits the exact same issue

Thanks all for your help