简体   繁体   中英

Undefined property error when using model

I'm a newbie using Codeigniter, I have a login controller and a login model in CI, why do I get this error?

" Undefined property: Login::$Login_model "

Error in Line 44 in "login" Controller:

if ($this->Login_model->check_user($username, $password) == TRUE)

This is my "login" Controller.

<?php 
 class Login extends CI_Controller {
 public function __login()
 {
    parent::__construct();
    $this->load->model('Login_model', '', TRUE);    
 }

 function index()
 {
    if ($this->session->userdata('login') == TRUE)
    {
        redirect('home');
    }
    else
    {
        $this->load->view('login/login_view');
    }
 }

 function process_login()
 {
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');      
    if ($this->form_validation->run() == TRUE)
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if ($this->Login_model->check_user($username, $password) == TRUE)
        {   
            //$datalevel = $this->Login_model->check_user($username);
            $data = array('username' => $username, 'login' => TRUE);
            $this->session->set_userdata($data);
            redirect('home');
        }
        else
        {
            $this->session->set_flashdata('message', 'Username dan/atau password Anda salah');
            redirect('login/index');
        }
    }
    else
    {
        $this->load->view('login/login_view');
    }
 }

 function process_logout()
 {
    $this->session->sess_destroy();
    redirect('login', 'refresh');
 }

And This is my "login_model" model

<?php
class Login_model extends CI_Model {
  function Login_model()
  {
    parent::__construct();
  }

  var $table = 'user';

  function check_user($username, $password)
  {
    $query = $this->db->get_where($this->table, array('username' => $username, 'password' => $password), 1, 0);

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

It says that your Model is not loaded. You can fix this by changing name of below method

public function __login()

to

public function __construct()

This way it will be automatically called when your class is called and your Model will be automatically loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM