CodeIgniter Forums
dynamic $config array Concept - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: dynamic $config array Concept (/thread-30266.html)



dynamic $config array Concept - El Forum - 05-08-2010

[eluser]Mark LaDoux[/eluser]
Ok, this is just a concept, and isn't in actual use anywhere. This may not be the wisest or best solution, so use at your own risk.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Retrieves configurations from database
*/

// This line is here just in case the database config hasn't been loaded yet,
// however, by adding the _once to it, it won't reload it if it's already in
// memory.
include_once(APPPATH.'config/database.php');

// Open our database connection
$con    = mysql_connect(
            $db[$active_group]['hostname'],
            $db[$active_group]['username'],
            $db[$active_group]['password']
);
mysql_select_db($db[$active_group]['database']);

// run our query
$sql        = "SELECT * FROM 'config'";
$cfg_qry    = mysql_query($sql, $con);

// grab an associative array and iterate through it
while ($row = mysql_fetch_assoc($cfg_qry))
{
    // set our config data
    $config[$row['name']]    = $row['value'];
}



dynamic $config array Concept - El Forum - 05-08-2010

[eluser]Jamie Rumbelow[/eluser]
Hi Mark,

Thanks for the contribution. Have you looked into extending the Config library and adding this? Many developers would find it extremely useful Smile

Jamie


dynamic $config array Concept - El Forum - 05-08-2010

[eluser]Mark LaDoux[/eluser]
Well, I did it as an add-on config so as not to do too much tweaking with the base itself, but it shouldn't be too difficult to add it on to the base config library. I'll get around to setting a library extension that does this sometime tomorrow, and post it up for anyone who's interested. It is really a very simple modification.


dynamic $config array Concept - El Forum - 05-08-2010

[eluser]Jelmer[/eluser]
I'd suggest extending the Controller class as MY_Controller (howto in the user guide, at the bottom on extending native libraries) and doing it like this (requiring far less code):
Code:
class MY_Controller extends Controller {
    function __construct()
    {
        parent::Controller();
        $configs = $this->db->get('config')->result_array();
        foreach ($configs as $c)
            $this->config->set_item($c['name'], $c['value']);
    }
}

Or use a hook and implement it as an "post_controller_constructor".
The reason you do it after the controller constructor in both cases is because the database class has been autoloaded at that time if you include it in the autoload.php, and there's little reason not to when you need DB access anyway.

Or skip the need for a DB query altogether and do it like I explained in this topic.


dynamic $config array Concept - El Forum - 05-08-2010

[eluser]Mark LaDoux[/eluser]
Jelmer: Thanks, now I don't have to do all that work, lol. Plus that is a lot simpler than extending CI_Config!


just a thought, I think I would have to extend CI_Model as well in order to make the configs available there.


dynamic $config array Concept - El Forum - 05-08-2010

[eluser]Jelmer[/eluser]
The config class should be available within models, that shouldn't be a problem. And in libraries you can access config values by replacing $this-> with get_instance()->, like this (in PHP5):
Code:
get_instance()->config->item('some_item');
And in PHP4 the following should work if I remember correctly:
Code:
$ci =& get_instance();
$ci->config->item('some_item');



dynamic $config array Concept - El Forum - 05-09-2010

[eluser]Mark LaDoux[/eluser]
that's not what I'm saying, I know the config class is available within models, I'm just not sure that the models will be able to access config items held in the database unless I modify the Model class in the same fashion as the Controller class. I might test this out later. Normal configs will definitely still be available, that isn't in question.


dynamic $config array Concept - El Forum - 05-10-2010

[eluser]Jelmer[/eluser]
It shouldn't be necessary. All models are called from the controller and as long as your controller extends the new MY_Controller all config items will already have been loaded by the MY_Controller constructor. And all items registered with the config class are available everywhere from the config class.


dynamic $config array Concept - El Forum - 05-10-2010

[eluser]steelaz[/eluser]
A while ago I extended Config library to use database - http://ellislab.com/forums/viewthread/131762/. It's not exactly the same solution as you're suggesting here, but it works for me. Most of applications I work on have settings page and in some cases these settings overwrite original hard-coded config values. I always use this extended Config library in my settings page.


dynamic $config array Concept - El Forum - 05-10-2010

[eluser]Jelmer[/eluser]
@steelaz that's an even better solution when you're loading config values from the database, more structurally sound the suggestion I posted as it works from the library where it belongs and includes updating & adding.

Though I personally prefer the approach of exporting the config values to an autoloaded config file, when you don't have a problem with the extra DB query this would indeed be the way to go.