0

我正在使用 CI 2.1.3,但在加载模型和引用 CodeIgniter“超级对象”时遇到问题。

例如:

我正在尝试使用我的登录控制器登录:

class Login extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Users_model');
    }

    public function validate_credentials() {
        $this->load->library('form_validation');
        // Some form validation here...

        $query = $this->Users_model->validate();

        if($query) {    // if the user's credentials validated...
            // something
        } else {
            // something
        }
    }

当它到达我的 Users_model 时:

class Users_model extends CI_Model {

public function __construct()
{
    parent::__construct();
}

public function validate()
{
    $this->db->where('active', 1);
    $this->db->where('email', $this->input->post('email'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('user');

    if($query->num_rows == 1)
    {
        return $query;
    }

}
}

我收到错误“致命错误:在第 XX 行的 users_model.php 中的非对象上调用成员函数 where()”,指的是 validate() 函数的第一行。

我可以通过在像 Users_model::validate() 这样的登录控制器中使用双冒号 (::) 运算符来使其工作,但我认为我不应该需要它。

我也可以通过编写如下内容来使其工作:

    $ci = get_instance();
    $ci->db->where...

在 Users_model->validate() 函数的开头,但我认为我也不应该这样做。

数据库类正在 autoload.php 中加载。

所以问题是我的模型中的$this指的是模型类本身,而不是它应该的“超级对象”。我已经没有动作了,我猜这是关于一些非常简单的事情,但我就是看不到它。请帮我。

4

1 回答 1

1

请尝试加载这样 的CI 库

  $this->load->library('database');

在加载模型之前在 __construct() 函数中:或从自动加载文件加载此库,例如:

$autoload['libraries'] = array('database','form_validation', 'session');
于 2013-03-09T12:42:11.913 回答