codeigniterhmvccodeigniter-hmvc

Codeigniter HMVC + ion_auth trouble loading the config items


I have been banging my head for 5 hours and I finally solved the problem but I just cannot go to sleep without knowing the reason. Let me explain the issue first.

I have used codeigniter HMVC extension and installed ion_auth as a separate module.

|-modules
|--auth
|---config
|-----ion_auth.php
|---controllers
|-----auth.php
|---models
|-----ion_auth_model.php
|---views

When I was trying to get a user's group I started to get wired SQL errors. Then I narrowed the issue and figured out that the items in config/ion_auth.php were not loaded in the ion_auth_model.php file.

ERROR - 2016-02-24 20:09:26 --> Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as id, .name, .description JOIN ON .=.id WHERE . = '2'' at line 1 - Invalid query: SELECT . as id, .name, .description JOIN ON .=.id WHERE . = '2'

Then I tried couple of stuffs and when I remove the index 'ion_auth' from couple of method calls in ion_auth_model.php everything started to work.

I changed

$this->tables  = $this->config->item('tables', 'ion_auth');
$this->join            = $this->config->item('join', 'ion_auth);

to

$this->tables  = $this->config->item('tables');
$this->join            = $this->config->item('join');

Can anyone tell me why it worked?


Solution

  • This is the inner implementation of the CodeIgniter function config->item() found in the file system\core\Config.php

    /**
     * Fetch a config file item
     *
     * @param   string  $item   Config item name
     * @param   string  $index  Index name
     * @return  string|null The configuration item or NULL if the item doesn't exist
     */
    public function item($item, $index = '')
    {
        if ($index == '')
        {
            return isset($this->config[$item]) ? $this->config[$item] : NULL;
        }
    
        return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
    }
    

    When you pass the $index parameter, the function checks if both parameters are initialized in the config and returns config[$index] of the CI instance; or null if any of them is not initialized.

    If config[$item] is not set in the CI instance, the function returns always null. Let's assume this is not the case, as your call don't crash when avoiding $index.

    So when you pass $index as the second parameter, your code crashes because the function returns null, and that means that the config[$index] of the CI instance is not set. Now the question is why it's not set, and I can't help you here, but it looks like you are missing to load some modules.

    Best regards