4

I am building an application using the latest copy of Codeigniter 2.0. My application is dynamic and is kind of like a custom CMS I guess you could say. I have a database table called 'settings' with the following fields:

  • id
  • name
  • value

Basically what I am currently doing is using a helper function to retrieve specific settings from my settings table like the site name or current theme. However I've started thinking that maybe the constant amount of database calls for retrieving settings is a bit too much on the database.

Is there a way of retrieving settings for my application from the database and then appending them to my configuration file? I've noticed Mojomotor does something similar and it is a CI 2.0 application, however I would much rather the simplest and easiest code to do so.

I would preferably like to be able to check every so often if a setting in the database has changed and update the configuration file. The less strain on the database the better.

4
  • Who is changing the database values, and why can't that person just change a configuration file instead? Commented Feb 2, 2011 at 2:13
  • 2
    @Dolph - when building a CMS, often you want non-technical users to be able to change a setting directly from a web page, without mucking around with text editors and FTP and syntax issues. Commented Feb 2, 2011 at 2:25
  • 1
    @Dolph - Summer is right. Editing a configuration file would be the ideal way, but there are just some people out there who can't understand simple instructions like: open config.php file, change values between quotes and then save. Some people just can't comprehend that sort of stuff, plus people are acustomed to being able to change and add settings from within their CMS's Commented Feb 2, 2011 at 2:33
  • Presumably this would sit alongside the normal CI config file? Commented Feb 2, 2011 at 10:29

1 Answer 1

6

The best solution lies in the middle. Not zero DB calls; and not one DB call per setting. Do one DB call per page load instead, and get every setting in a recordset / object that the rest of your app can refer to as needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Genius idea actually. So perhaps create a public variable called $settings and make it an array, then in my constructor I can simply fetch all settings and put them in the array in MY_Controller which all other controllers on the site extend and then get a setting like so: $this->settings['site_theme'] which in this case if the theme was called 'icy' then it would return icy. I'd imagine performance impact would be low as there would be no more than 50 rows in the settings table, maybe 8 or so out of the box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.