【问题标题】:Call to member function get_where() on a Non Object in file UserFactory在文件 UserFactory 中的非对象上调用成员函数 get_where()
【发布时间】:2015-10-28 19:15:27
【问题描述】:

问题

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

用户模型

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

问题

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

【问题讨论】:

    标签: php codeigniter codeigniter-2 codeigniter-3


    【解决方案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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-26
      • 2012-04-20
      • 2015-05-01
      • 2014-08-07
      • 2013-12-01
      • 2015-12-23
      • 2015-08-29
      • 2015-02-02
      相关资源
      最近更新 更多