Search code examples
phpcodeignitercodeigniter-2codeigniter-3

Call to member function get_where() on a Non Object in file UserFactory


Problem

When action method gets executed...Error Occurs.. Call to member function get_where() on a Non Object in file UserFactory.

User Model

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

User Factory

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

Controller Action Method

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

Problem

When action method gets executed...Error Occurs.. Call to member function get_where() on a Non Object in file UserFactory.


Solution

  • I'm assuming its this line of code?

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

    Anyhow, this has been answered on here many times.

    Maybe try this: 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]