Welcome Guest, Not a member yet? Register   Sign In
Bug and solution in set_item method of CI_Config Class
#1

[eluser]Biktor[/eluser]
A few hours ago I posted a question about the pagination and the config libraries.

Studying the problem, I found that the method set_item() in the CI_Config class has missing the index parameter (used in the other methods to group settings in a separate index of the config array)

For example, to obtain an array element the CI_Config class has the method item() as follows:

Code:
/**
     * Fetch a config file item
     *
     *
     * @access    public
     * @param    string    the config item name
     * @param    string    the index name
     * @param    bool
     * @return    string
     */
    function item($item, $index = '')
    {    
        if ($index == '')
        {    
            if ( ! isset($this->config[$item]))
            {
                return FALSE;
            }

            $pref = $this->config[$item];
        }
        else
        {
            if ( ! isset($this->config[$index]))
            {
                return FALSE;
            }

            if ( ! isset($this->config[$index][$item]))
            {
                return FALSE;
            }

            $pref = $this->config[$index][$item];
        }

        return $pref;
    }

To assign a new value to an item, the CI_Config class has set_Item() method as follows:

Code:
/**
     * Set a config file item
     *
     * @access    public
     * @param    string    the config item key
     * @param    string    the config item value
     * @return    void
     */
    function set_item($item, $value)
    {
        
        $this->config[$item] = $value;
    }

It is impossible to assign a value to an item that is within an index by this method.

I solved the problem by extending the class and changing the method for this one (the business logic I took from the item() method):

Code:
/**
     * Set a config file item
     *
     * @access    public
     * @param    string    the config item key
     * @param    string    the config item value
     * @param    string    the index name
     * @return    void
     */
    function set_item($item, $value, $index = '')
    {
        if ($index == '')
        {    
            if ( ! isset($this->config[$item]))
            {
                return FALSE;
            }
            $this->config[$item] = $value;
        }
        else
        {
            if ( ! isset($this->config[$index]))
            {
                return FALSE;
            }

            if ( ! isset($this->config[$index][$item]))
            {
                return FALSE;
            }
            $this->config[$index][$item] = $value;
        }
    }

My English is not very good as you can see, I apologize in advance if I made a mistake.
#2

[eluser]jfurey[/eluser]
Fantastic - I just came across this roadblock too.

Team CI - please add to your next build! :-P




Theme © iAndrew 2016 - Forum software by © MyBB