CodeIgniter Forums
Could not load model over Autoload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Could not load model over Autoload (/thread-64321.html)



Could not load model over Autoload - Maliko - 02-08-2016

Hey,

i have a little strange Problem. When i try to load a model over the Autoload.php and try to use it, i'll become an Error that the model could not found (Undefined property: Home::$users_to_groups), but when i load it over $this->load->model() it works. But why?

Here is the Code that i Use:
PHP Code:
<?php

class Rights_management {
 
   private $_ci null;
 
   private $_aGroupIds = [];

 
   public function __construct() {
 
       $this->_ci get_instance();

 
       //$this->_ci->load->model('rights/Users_to_groups_model', 'users_to_groups');
 
       $this->_aGroupIds $this->_getUserInformation();
 
   }

 
   private function _getUserInformation() {
 
       $aAccountData element('accountData'$this->_ci->session->userdata());

 
       if(!empty($aAccountData)) {
 
           $iUserId element('id'$aAccountData);

 
           return array_column($this->_ci->users_to_groups->getGroupIds($iUserId), 'group_id');
 
       }

 
       return [];
 
   }

 
   public function checkAccess($sCode) {
 
       return $this->_ci->groups_to_rights->checkAccess($sCode$this->_aGroupIds);
 
   }


Has anywhone an Idea what i do wrong? When i insert the commented Line, than the script is full functionally (the groups_to_rights modal can be found).


RE: Could not load model over Autoload - ciadmin - 02-08-2016

Your class needs to extend CI_Model.
http://www.codeigniter.com/user_guide/general/models.html


RE: Could not load model over Autoload - Maliko - 02-08-2016

No. The Class that i linked is a Library to a Model.


RE: Could not load model over Autoload - mwhitney - 02-15-2016

How are you listing the model in your config/autoload.php file? To get the same result as this:

PHP Code:
$this->_ci->load->model('rights/Users_to_groups_model''users_to_groups'); 

You need to use something like this:

PHP Code:
$autoload['model'] = ['rights/Users_to_groups_model' => 'users_to_groups']; 

However, it should also be noted that this depends on where you are attempting to use the model. Classes in the autoload config are not loaded until the Controller's constructor has been executed, so some hooks will execute before the model is loaded.