在文件UserFactory中的非对象上调用成员函数get_where()

时间:2015-10-28 19:15:28

标签: php codeigniter codeigniter-2 codeigniter-3

问题

  

执行操作方法时...错误发生..调用成员   函数get_where()在文件UserFactory中的非对象上。

用户模型

<?php
    class User_Model extends CI_Model {

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

        private $_username;
        private $_password;

        public function getUsername()
        {
            return $this->_username;
        }

        public function setUsername($value)
        {
            $this->_username = $value;
        }       
    }
?>

用户工厂

<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class UserFactory {

        private $_ci;

        function __construct()
        {
            $this->_ci =& get_instance();
            //Include the user_model so we can use it
            $this->_ci->load->model("User_Model");
        }
        public function checkLogin($userName, $password) {
            //Getting an individual user
            $query = $this->_ci->db->get_where("panel_login",
                          array("username" => $userName, "password" => $password));
            if ($query->num_rows() > 0) {
                return $this->createObjectFromData($query->row());
            }
            return null;            
        }

        public function createObjectFromData($row) {
            $user = new User_Model();
            $user->setUsername($row->username);
            return $user;
        }
    }
?>

控制器操作方法

public function AuthenticateUser() {
    //Is the UserName and Password values retrieved?
    if( isset( $_POST['userName'] ) && isset( $_POST['password'] ) ) {
        $this->load->library("UserFactory");
        //Get User details based on UserName and Password
        $userName = addslashes($_POST['userName']);
        $password = addslashes($_POST['password']);

        $data = array(
            "users" => $this->userfactory->checkLogin($userName, $password)
        );

        header('Content-Type: application/json');
        echo json_encode( $data );
    }
    else {
        header('Content-Type: application/json');
        echo json_encode( 'UserName or Password cannot be blank' );
    }
}

问题

  

执行操作方法时...错误发生..调用成员   函数get_where()在文件UserFactory中的非对象上。

1 个答案:

答案 0 :(得分:1)

我假设这行代码?

$query = $this->_ci->db->get_where("panel_login", array("username" => $userName, "password" => $password));

无论如何,这已经在这里多次回答了。

也许试试这个:Fatal error: Call to a member function get() on a non-object in C:\wamp\www\ci\application\models\site_model.php on line 6

OR

CodeIgniter Call to a member function get_where() on a non-object [duplicate]