Welcome Guest, Not a member yet? Register   Sign In
Getting Call to a member function where() on a non-object.
#1

[eluser]Gork[/eluser]
For some reason I'm going through one of the video tutorials about logins and I can seen to get this error to go away. I'm loading my model which users.php. I'll just post the code and please tell me what I'm doing wrong.

My Model class is here

Code:
<?php
/**
* User Model
*
* @package User
*
*/
class Users extends CI_Model
{
/*Utility Methods*/
/*Determines if a field is required or not*/
public function _required($required, $data)
{
  foreach ($required as $field)
   if (!isset($data[$field])) return false;

  return true;
}

public function _default($defaults, $options)
{
  return array_merge($defaults, $options);
}
/*Constructor calling construct of CI_Model*/
public function __construct()
{
  parent::__construct();
}

public function AddUser($options = array())
{
  //check to make sure user_email and user_password are required
  if (!$this->_require(array('user_email', 'user_password'), $options))
   return false;
  $options = $this->_default(array('user_status', 'active' ), $options);
  $this->db->insert('users', $options);

  return $this->db->insert_id();
}

/**
  * Update method to update records in the User Model
  *
  * Options
  * ----------------------------------
  *
  * user_id
  * user_email
  * user_password
  *
  * @param array
  * @return int row_affected
  */
public function UpdateUser($options = array())
{
  //check to make sure user_email and user_password are required
  //Set values for update
  if (!$this->_require(array('user_id'), $options))
   return false;

  if (isset($options['user_email']))
   $this->db->set('user_email', $options['user_email']);

  if (isset($options['user_password']))
   $this->db->set('user_password', md5($options['user_password']));
  
  if (isset($options['user_status']))
   $this->db->set('user_status', $options['user_status']);
  

  $this->db->update('users');

  return $this->db->affected_rows();
}

/**
  * GetUsers method gets a qualified list of users
  *
  * Options
  * --------------------------------
  * user_id
  * user_email
  * user_password
  * user_status
  *
  * limit    limit to a number of records
  * offset    bypass this many records
  * order by    order by a column
  * order direction  order (asc, desc)
  *
  * Returned Object (array)
  *
  * user_id
  * user_email
  * user_password
  * user_status
  *
  * @param options array
  * @return array objects
  */
public function GetUsers($options = array())
{
  //Qualifications Select statements
  if (isset($options['user_id']))
    $this->db->where('user_id', $options['user_id']);

  if (isset($options['user_email']))
        $this->db->where('user_email', $options['user_email']);

  if (isset($options['user_password']))
   $this->db->where('user_password', $options['user_password']);

  if (isset($options['user_status']))
   $this->db->where('user_status', $options['user_status']);
  
  //limit/offset
  if (isset($options['limit']) && isset($options['offset']))
   $this->db->limit($options['limit'], $options['offset']);
  else if (isset($options['limit']))
   $this->db->limit($options['limit']);
  
  //sort
  if (isset($options['sortBy']) && isset($options['sortDirection']))
   $this->db->order_by($options['sortBy'], $options['sortDirection']);
  
  $query = $this->db->get('users');
  
  if (isset($options['user_id']) || isset($options['user_email']))
   return $query->row(0);
  
  return $query->result();
}
}

And my Controller is this, which if you notice I'm loading the model. I have database autoload in my autoload.php file.

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

class Main extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('users');
    }

    public function login()
    {
        $this->form_validation->set_rules('user_email', 'email', 'trim|required|valid_email|callback__check_login');
        $this->form_validation->set_rules('user_password', 'password', 'trim|required');

        if ($this->form_validation->run()) {
            //Form has been successfully submited

        }

        $this->load->view('main/login_form');

    }

    public function index()
    {
        $this->load->view('main/index');
    }

    public function _check_login($user_email)
    {
        if ($this->input->post('user_password')) {
            $user = $this->users->GetUsers(array('user_email' => $user_email, 'user_password' => md5($this->input->post('user_password'))));
            if ($user)
                return true;
        }
        $this->form_validation->set_message('_check_login', 'Your username / password combination is invalid');
        return false;

    }
}

/* End of file main.php */
/* Location: ./application/controllers/main.php */


And this is my login view to the login

Code:
<?=form_open(base_url() . 'main/login')?>
<fieldset>
<legend>Login Form</legend>
<ul>
  <li>
   <label>User-Email</label>
   &lt;?=form_input('user_email','user_email')?&gt;
   &lt;?=form_error('user_email')?&gt;
  </li>
  <li>
   <label>User-Password</label>
   &lt;?=form_password('user_password')?&gt;
   &lt;?=form_error('user_password')?&gt;
  </li>
  <li>
   &lt;?=form_submit('', 'Submit')?&gt;
  </li>
</ul>
</fieldset>
&lt;?=form_close()?&gt;
#2

[eluser]InsiteFX[/eluser]
Did you load the database?
#3

[eluser]Gork[/eluser]
Yes I've autoloaded the database in the autoload.php file.
#4

[eluser]InsiteFX[/eluser]
Read this:
isset() VS empty()
#5

[eluser]Gork[/eluser]
Ok I'm lost now. I've did comparison between the video which is on codeigniter video tutorials by *Shawn McCool’s Blog login process and my code and his set the same way as mine. Here's a link to the video. http://shawnmccool.com/2009/11/09/develo...n-process/




Theme © iAndrew 2016 - Forum software by © MyBB