codeigniter access model from library

2020-06-01 08:27发布

I am trying to integrate the following code into my project. it is held in a library

function do_std_login($email, $password) {
    $CI =& get_instance();
    $login = $CI->users_model->login($email, md5($password));
    if($login){
        $session_array = array(
            'user_id' => $login->user_id,
            'name' => $login->name,
            'type' => 'Standard'
        );
        $CI->session->set_userdata($session_array);

        // Update last login time
        $CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);

        return true;
    } else {
        $this->errors[] = 'Wrong email address/password combination';
        return false;
    }
}

I am calling it this way:

$login = $this->jaclogin->do_std_login($this->input->post('email'),$this->input->post('password'));

but when I run it I get the following error

A PHP Error was encountered Severity: Notice Message: Undefined property: Login::$users_model Filename: libraries/jaclogin.php Line Number: 45

I have check I am do load the correct library in the codeigniter autoload file.

Any Ideas?

Thanks

Jamie Norman

标签: codeigniter
1条回答
Explosion°爆炸
2楼-- · 2020-06-01 09:03

Using your CI instance, load your model explicitly in the library like so..

function do_std_login($email, $password) {
    $CI =& get_instance();
    //--------------
    $CI->load->model('users_model');  //<-------Load the Model first
    //--------------
    $login = $CI->users_model->login($email, md5($password));
    if($login){
        $session_array = array(
            'user_id' => $login->user_id,
            'name' => $login->name,
            'type' => 'Standard'
        );
        $CI->session->set_userdata($session_array);

        // Update last login time
        $CI->users_model->update_user(array('last_login' => date('Y-m-d H:i:s', time())), $login->user_id);

        return true;
    } else {
        $this->errors[] = 'Wrong email address/password combination';
        return false;
    }
}
查看更多
登录 后发表回答