Sunday, January 30, 2011

Re: Configuration

Create a database table with the correct fields, perhaps called
'Settings'.

You can then create (or bake) a model and controller to go with the
table.

The controller only needs an edit action. The action should get the id
for the model from the applications configuration, rather than a
variable passed to it like normal. Here's what I mean:

class SettingsController extends AppController {

var $name = 'Settings';

function admin_edit() {
if (empty($this->data)) {
$this->data = $this->Setting->read(null,
Configure::read('settingsId'));
} else {
if ($this->Setting->save($this->data)) {
Cache::delete('settings');
$this->Session->setFlash('Settings saved.');
$this->redirect('/');
} else {
$this->Session->setFlash('Settings could not be saved. Please try
again.');
}
}
}
}

Notice that Configure::read('settingsId') is used to provide the id of
the row in the database.

So in /config/core.php you could put:

Configure::write('settingsId', 1);

Where 1 is the id of the row in the database.

You can create your view for the edit action like normal.

You should now have a way for site administrators to edit settings -
and they will be validated by the model.

When it comes to using the settings they need to be fetched from the
database. To make them available to each controller use the
AppController's beforeFilter method. This will make sure they are
ready before the appropriate action is executed.

However, fetching them every time a page is viewed is probably
uneccessary, so they should be cached. You may have noticed that in
the SettingsController I pasted above there is a line:
Cache::delete('settings'); this makes sure that when the settings are
edited, the old ones are removed from the cache.

Here is an example of the AppController:

class AppController extends Controller {

var $uses = array('Setting');

function beforeFilter() {
//fetch site settings
$this->settings = $this->_getSettings();
}

function _getSettings() {
//check to see if they are in the cache.
if (!$cachedSettings = Cache::read('settings', 'long')) {
//fetch the settings from the database.
$result = $this->Setting->read(null, Configure::read('settingsId');
//if no site found exit;
if (!$result) exit();
$settings = array_shift($result);
Cache::write('settings', $settings);
return $settings;
} else {
//return the cached settings.
return $cachedSettings;
}
}
}

Now you can access the settings elsewhere in your application by using
$this->settings. For example you may wish to pass the site name to the
view:

$this->set('siteName', $this->settings['siteName']);

I hope this helps.

On Jan 30, 9:35 am, elias <elias.leco...@gmail.com> wrote:
> Hi,
>
> How would I best solve this problem:
>
> I have a couple of variables I would want to store 'somewhere', and
> will access a lot.
> Variables like:
> * sitename
> * prefered thumbnail width (so a couple of ints)
> * a litlle text to show as message on the website
> * probably more
>
> The idea is that this information should be possible with a form, when
> an administrator is logged in.
> The ability of changing is very important.
>
> thx

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: