tags:

views:

655

answers:

3

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*

+1  A: 

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/

Summer
+4  A: 

Hello,

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

Pedro
Yes, but loading libraries where they're needed minimizes dependency issues.
Summer
So load it in the just in controller function that call the model
Pedro
What a crappy idea! Seriously, Another short sighted blunder by the CodeIgniter team! A model is a perfect place to consume an API library that deals with getting application data! Force me to load a library in the controller that an underlying Model class depends on!?? What the hell? Screw that! Just call $this->_assign_libraries(); done!
Bretticus
A: 

Libraries should automatically be assigned to the Model instance so it should work fine.

Remember if you can't access the super-global you can always use $ci =& get_instance() to grab it at no extra cost to your memory.

But still... your code example should work >.<

Phil Sturgeon