【问题标题】:CodeIgniter, Call to a member function post() on a non-objectCodeIgniter,在非对象上调用成员函数 post()
【发布时间】:2013-12-23 15:05:25
【问题描述】:

我是 CodeIgniter (v.2.1.4) 的新手。我在从模型中获取帖子时遇到问题。

我有一个控制器:

class Login extends CI_Controller{

    public function index(){
        $data['main_view'] = 'login_form';
        $this->load->view('login/include/template', $data);
    }

    public function validate(){

        $this->load->library('form_validation');
        $this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
        $this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');

        if($this->form_validation->run()){
            $this->load->model('users_model');

            if($this->users_model->validate_login($username, $password)){
                //valid user
            }
            else{
                //not vlaid user
            }
        }
        else{
            $this->index();
        }
    }
}

还有一个模型:

class Users_model extends CI_Model{

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

    function validate_login(){
        $username = $this->input->post('user');
        $password = md5($this->input->post('pass'));

        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('users');

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

当通过表单(post)有效对(用户名和密码)发送时,没有任何反应,但在 apache2 日志中出现:

PHP 致命错误:在 /var/www/ci/application/models/users_model.php 中的非对象上调用成员函数 where()

怎么了?

【问题讨论】:

标签: php codeigniter-2


【解决方案1】:

为登录类添加构造函数

class Login extends CI_Controller {
    public function __construct() {
        parent::__construct();
    }
}

【讨论】:

  • 不应该有任何区别,如果子级没有设置构造函数(重载),则默认调用父构造函数。
  • 登录类扩展了 CI_Controller,因此为了继承这些方法,您需要有一个构造函数。
  • 拥有一个仅用于调用父构造函数的构造函数是没有意义的,只有在构造函数中有附加代码时才需要它。见:codepad.org/wO07H2Y5
  • 你还用codeigniter吗?他在使用 CI 的方法,所以他必须这样做。
  • 是的,我不知道发生了什么
【解决方案2】:

您是否已加载数据库?您需要先加载数据库。

您可以将其添加到 /config/autoload.php 以自动加载数据库功能:

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

或者像这样按需调用:

$this->load->database();

更多详情here

【讨论】:

  • 数据库已加载:$autoload['libraries'] = array('database', 'session');如果我用帖子评论行,下一个错误是调用非对象上的成员函数 where(),我认为问题出在模型中?
  • 您可以尝试像这样在使用 where() 之前尝试像这样内联加载数据库:$this->load->database();
  • 问题不在于数据库,另一个错误是:PHP Fatal error: Call to a member function post() on a non-object.
【解决方案3】:

不要在模型中使用 post 而是在控制器中使用 this

控制器登录.php

public function validate(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
    $this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');

    if($this->form_validation->run()){
        $this->load->model('users_model');

        $post = $this->input->post();

        $data['username']   =   $post['username'];
        $data['password']   =   md5($post['password']);
        if($this->users_model->validate_login($data)){
            //valid user
        }else{
            //not vlaid user
        }
    }
    else{
        $this->index();
    }
}

型号

function validate_login($where){
    $this->db->where($where);
    $query = $this->db->get('users');

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

这应该可行。请记住,以某种方式调用 post 在模型中不起作用。我不完全知道他们的原因。

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 2013-08-23
    • 2012-01-09
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    相关资源
    最近更新 更多