CodeIgniter Forums

Full Version: Could not load model over Autoload
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

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:

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).
Your class needs to extend CI_Model.
No. The Class that i linked is a Library to a Model.
How are you listing the model in your config/autoload.php file? To get the same result as this:

PHP Code:

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.