Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter access model from library

Tags:

codeigniter

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

like image 517
JaChNo Avatar asked Jul 22 '11 20:07

JaChNo


People also ask

How check model loaded or not in CodeIgniter?

$config['log_path'] = 'another/directory/logs/'; CI will then create the log file in the directory. monitor the log files as you like. You can get the debug message to see if your model is already loaded or not in the log files.

Where do I put library in CodeIgniter?

All of the available libraries are located in your system/libraries/ directory. In most cases, to use one of these classes involves initializing it within a controller using the following initialization method: $this->load->library('class_name');

What is use of libraries in CodeIgniter?

CodeIgniter has rich set of libraries, which you can find in system/libraries folder but CodeIgniter is not just limited to system libraries, you can create your own libraries too, which can be stored in application/libraries folder. You can create libraries in three ways.


1 Answers

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;
    }
}
like image 182
jondavidjohn Avatar answered Oct 11 '22 16:10

jondavidjohn