Monday, January 31, 2011

Re: Configuration

It was easy.
I just had to use loadModel to make sure the Setting model is loaded, also in different controllers.

<?php
class AppController extends Controller {
    function beforeFilter() {
        $this->loadModel('Setting');
        $this->pfSettings = $this->getSettings();
        $this->set('site_name', $this->pfSettings['name']);
    }
   
    function getSettings()
    {       
        //check to see if they are in the cache.
        if (!$cachedSettings = Cache::read('settings', 'long')) {
            //fetch the settings from the database.
            if($settings = $this->Setting->find('all')) {
                $configurations = array();
                foreach ($settings as $setting):
                    //$configurations = array_merge(array($setting['Setting']['key']=>$setting['Setting']['value']));
                    $configurations[$setting['Setting']['key']] = $setting['Setting']['value'];
                endforeach;
                Cache::write('settings', $configurations);
                return $configurations;
            }
            else
            {
                exit();
            }
        } else {
            //return the cached settings.
            return $cachedSettings;
        }
    }
}

--
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: