简体   繁体   English

CodeIgniter:调用模型时的未定义属性

[英]CodeIgniter: Undefined property when calling the model

I use CodeIgniter 3 with HMVC. 我将HMVC与CodeIgniter 3一起使用。

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". 哪里崩溃了,因为没有“ db”。 What is the new solution for CodeIgniter 3? 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() { 公共功能__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. 问题已解决,问题是由我的控制器扩展了CI_Controller而不是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. 我花了一些时间通过调试thirdparty / MX / Loader.php来解决这个问题,但是一旦我看到它正在寻找MX_Controller类型,便进行了更改,并且效果很好。

This issue is one in many related to migration from CI 2 to CI 3 and also using the HMVC from Wiredesignz. 这个问题与从CI 2迁移到CI 3以及使用Wiredesignz的HMVC有关。 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"): 另一个大问题是文件名是大写的,调用时是大写的,因此严格地讲这个问题,我不得不在控制器中将调用大写(将“ login”更改为“ 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 感谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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