Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a library in a model in CodeIgniter

Tags:

codeigniter

Why won't my model load the encryption library?

class User_model extends Model {

  function User_model() {
    parent::Model();
    $this->check_login();
  }

  function check_login() {
    $this->load->library('encrypt');
    $email = $this->encrypt->decode($email);
    ....
  }
}

This giving me a PHP error: Call to a member function decode() on a non-object on line X -- where X is the $this->encrypt->decode($email); line?

Edited to show that the problem was that check_login was called from the constructor

like image 647
Summer Avatar asked Mar 02 '10 18:03

Summer


2 Answers

You don't need to load the library in the MODEL, MODELS are always called from the CONTROLLERS so you just have to load the Libraries in the Controller, and the functions will be available in the models called from him!

Regards,
Pedro

like image 191
Pedro Avatar answered Sep 30 '22 01:09

Pedro


I was calling check_login from within the constructor, and that was causing the problems.

The solution is to call $this->_assign_libraries(); right after loading a library in a constructor.

Thanks to this codeignitor forum thread: http://codeigniter.com/forums/viewthread/145537/

like image 29
Summer Avatar answered Sep 30 '22 00:09

Summer