CodeIgniter - 模型已加载但无法使用?

发布于 2024-08-20 11:22:08 字数 1030 浏览 7 评论 0原文

好的。所以我正在使用 CI 开发一个网站。这是我的控制器的结构:

class MY_Controller extends Controller
class User extends MY_Controller
class User_Model

因此,我将 User_Model 加载到 User 控制器的构造函数中。我知道它已正确加载,因为我尝试从 User_Model 打印一些内容并且它工作得很好。但是,当我使用用户控制器中的 User_Model 中的函数之一时,它开始出现错误。这是我收到的错误:

未定义的属性:User::$User_Model

有人有什么想法吗?

这是扩展控制器

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

这是控制器

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

这是 User_model

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最笨的告白 2024-08-27 11:22:08
$this->load->model('user_model');

应该阅读

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

模型名称区分大小写!

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

should read

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

Models names are case-sensitive!

攒眉千度 2024-08-27 11:22:08

您是否使用语法 $this->User_Model->function_name() 来调用该函数?

我还知道我过去也遇到过区分大小写的问题。

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.

悲欢浪云 2024-08-27 11:22:08

好吧,通过使用 MY_Controller 并直接扩展控制器的用户控制器,可以解决这个问题。

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

葬花如无物 2024-08-27 11:22:08

你把My_Controller文件放在哪里?我把我的放在 system/application/libraries 中,没有任何问题。另外,我使用 PHP4 构造函数方式来编写它,而不是 __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
    //...
  }

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文