CodeIgniter调用非对象上的成员函数get_where() [英] CodeIgniter Call to a member function get_where() on a non-object

查看:2568
本文介绍了CodeIgniter调用非对象上的成员函数get_where()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im super new codeigniter,我试图找出如何使用模型,ive使用一个MVP框架之前,但它是一个有点不同。我有用户控制器,它初始化模型(我认为我做的正确),然后模型有一个函数,通过电子邮件获取一行。

Im super new to codeigniter and am trying to figure out how to use the Models, ive used a MVP framework before but it was a bit different. I have the users controller, which initializes the model(I think I did it right), then the model has a function, that gets a row by email.

控制器如下所示:

class Users extends CI_Controller{
    public function login(){
        $this->load->model('users_model');

        $this->users_model->login('slavigne@uark.edu', 'abcdefg');

        $this->load->view('templates/header');
        $this->load->view('users/login');
        $this->load->view('templates/footer');
    }
}

,模型外观如下:

class users_model extends CI_Model{
    public function users_model(){
            parent::__construct();
    }

    public function login($email, $password){
        $query = $this->db->get_where('users', array('email' => $email));
        return $query->result();
    }
}

遇到的问题是:

Call to a member function get_where() on a non-object

这意味着db不是一个对象,而是我为模型看到的所有代码,说这应该工作。任何帮助或建议将是伟大的!还有任何关于新手错误codeigniter的建议是不错的!

which I understand means that db isn't an object, but all the code I saw for models, say that this should work. Any help or advice would be great! Also any advice on newbie mistakes to codeigniter would be nice!

推荐答案

你似乎错过了 $ this-> load-> database code>

You seem to miss $this->load->database();

class users_model extends CI_Model{
    public function users_model(){
            parent::__construct();
    }

    public function login($email, $password){
        $this->load->database();
        $query = $this->db->get_where('users', array('email' => $email));
        return $query->result();
    }
}

您也可以自动加载数据库库 application / config / autoload.php 文件。

You can also auto-load the database library in the application/config/autoload.php file.

$autoload['libraries'] = array('database');

如果这样做,您不需要每次都手动加载它,意味着它在没有进行数据库调用的页面上加载。

If you do so, you don't need to load it manually each time, but it will also mean it loads on pages where no database calls are made. Might be more of a care for optimizers to spare bandwidth.

此外,如果您使用多个数据库连接,您仍然应该使用 $ this-> load-> database(); tho。有关详情,请参阅此链接: http://ellislab.com/codeigniter/user- guide / database / connecting.html

Also if you are using more than one database connection, you should still use the $this->load->database(); tho. See this link for more info: http://ellislab.com/codeigniter/user-guide/database/connecting.html

$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

而不是使用..

$this->db->query();
$this->db->result();

..您将改为使用:

$DB1->query();
$DB1->result();
$DB2->query();
$DB2->result();

您仍然可以将加载添加到 __ construct(),所以每个控制器只需要一行。

You can still add the load to the __construct(), so you only need one line per controller.

这篇关于CodeIgniter调用非对象上的成员函数get_where()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆