Best practice for loading config items from DB

Hi Folks,

I'm developing a web site using CI which shows a different template and content depending on the header URL.

I want to store these config items in a database, and then to have them loaded into the config array once when a session starts.

Because some of these config tems are long (api urls, etc) and there are about 30 of them, i'd rather not store the config info with the session data or in cookies.

Can anyone suggest a nice way of doing this?

Many thanks


[eluser]Joshua Logsdon[/eluser]
If you don't want to query the database each page load, would something like querying and then caching the result for reference appeal to you?

Or if you are looking for just how to load items into the config, have you checked this out?

In particular at the end with:
$this->config->set_item('item_name', 'item_value');

You could loop over your results and store them. Although if you are using your results immediately in the same controller/method, you could just set variables for reference instead of the config.

Or if you are looking for how to do this consistently, you may do this work in your controller constructor. If you need it site-wide you may want to create a custom MY_Controller class (see Extending Native Libraries at that does the work in the constructor and saves to a property, then all your other controllers can extend it so that property is available in them.

Thanks for the reply Joshua,

I had failed to spot the method to dynamically set a config item. I think looping through the column names of my config table and using these as the $config array index names provides a very neat and scalable way of managing this particular problem.

Thanks for pointing me in the right direction!


In case anyone is wondering how I ended up doing this, here's what I did:

This function loops through all the columns in the Config table of the databse, and sets the appropriate value depending on the ClientID.
function load_config($id)
  $query = $this->db->get_where('Config', array('ClientID' => $id));
  $row = $query->row();
  foreach ($query->list_fields() as $field)
   $this->config->set_item($field, $row->$field);

It's called from my session function so this is only loaded once as a new session is created.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.