Welcome Guest, Not a member yet? Register   Sign In
Best practice for loading config items from DB
#1

[eluser]George36[/eluser]
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

George
#2

[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?
http://ellislab.com/codeigniter/user-gui...onfig.html

In particular at the end with:
Code:
$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 http://ellislab.com/codeigniter/user-gui...aries.html) 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.
#3

[eluser]George36[/eluser]
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!

George
#4

[eluser]George36[/eluser]
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.
Code:
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.




Theme © iAndrew 2016 - Forum software by © MyBB