CodeIgniter Forums
Access to libraries in MY_Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 2.x (https://forum.codeigniter.com/forum-18.html)
+--- Thread: Access to libraries in MY_Model (/thread-63107.html)



Access to libraries in MY_Model - scion - 09-27-2015

Hi! I face with problem discussed in this old post and solution never was found.
http://forum.codeigniter.com/archive/index.php?thread-40844-1.html

Funny that in MY_Controller everything is fine. Any suggestions? Maybe someone faced with it too?


RE: Access to libraries in MY_Model - InsiteFX - 09-27-2015

Get the CI super object.


RE: Access to libraries in MY_Model - scion - 09-28-2015

(09-27-2015, 04:20 PM)InsiteFX Wrote: Get the CI super object.

In CI_Model we have magic method


Code:
/**
     * __get
     *
     * Allows models to access CI's loaded classes using the same
     * syntax as controllers.
     *
     * @param    string
     * @access private
     */
    function __get($key)
    {
        $CI =& get_instance();
        return $CI->$key;
    }

So in MY_Model in usual case u dont need get super object cause if u dont have property or method - it will be looking at super object.

That is the first note. Second - even if I get super object clearly $CI =& get_instance(); and load library $CI->load->library('my_lib');

when i try to run some method $CI->my_lib->some_method it throws error like i mention before

Call to a member function some_method on a non-object in... MY_Model and so on...


RE: Access to libraries in MY_Model - InsiteFX - 09-28-2015

Try just loading the library in your controller then call your library methods in your model.


RE: Access to libraries in MY_Model - scion - 09-29-2015

(09-28-2015, 11:01 AM)InsiteFX Wrote: Try just loading the library in your controller then call your library methods in your model.

Thats why I write here - I try to load library in MY_COntroller (or Public_Controller or whatever Controller) and in MY_Model i get error describe above.

I dont understand why this is working so, but i found the solution. In base MY_Controller I get super object like next:


PHP Code:
$ci =& get_instance();
 
      
$ci
->login false;
 
      
$ci
->_user null;
 
      
 
if($this->ion_auth->logged_in() && !$this->ion_auth->is_admin())
 {
 
          $ci->login true;
 
          $ci->_user $this->ion_auth->user()->row();
 
 

After that I can assecc to login and user in any Controller or MY_Model or Model with simple $this->login and $this->_user

Thanks for participation in the discussion