Posted on 20/9/06 by Felix Geisendörfer
A lot of you probably like the idea of storing some of your applications configuration in a database since this way you can easily tweak things at run time versus having to edit configuration files on the server.
For me I found it pretty useful to just have a simple table called configs which contains two fields: "name" and "value". "name" is the primary key and it's type is varchar(255). The "value" fields type is text. After setting this up, I created a simple Model called Config and added two convenient little function named get & store to it which I can call from my controllers.
The code for the Config Model is this:
var $name = 'Config';
var $primaryKey = 'name';
var $validate = array('name' => VALID_NOT_EMPTY);
function get($name, $default = null)
$config = $this->findByName($name);
function store($name, $value)
return $this->save(array('name' => $name, 'value' => $value));
And usage is as simple as this:
var $uses = array('Config');
$this->theme = $this->Config->get('theme', 'summer');
So in the example above the beforeFilter tries to get the value of 'theme' out of the configs table and in case that fails (theme isn't set), it will return 'summer' as default value.
--Felix Geisendörfer aka the_undefined
You can skip to the end and add a comment.
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.