简体   繁体   中英

CodeIgniter: Undefined property when calling the model

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!

just try this code put in controller:

public function __construct() {

    parent::__construct();


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


}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM