Wednesday, April 11, 2012

Re: Site wide settings with Configure::write and read

AppController:
       function beforeFilter() {
              //Loading model on the fly
              $this->loadModel('Setting');
              //Fetching All params
        Configure::write('settings', $this->Setting-
>find('list', array ('fields' => array ('Setting.name',
'Setting.value'))));
       }

Everywhere then:
       Configure.read('settings.my_setting_name');

Changes:
1. Code without reference to unknown variables
2. Code is simpler by utilizing standard Cake features
3. Code is placed into callback so no need to call this method
manually (did you call your method at all?)
4. Settings are saved under general "settings" key to not mix with
other things may be placed in Configure (remove prefix if you need to
replace standard values such way - like "debug")

On 11 апр, 18:33, JonStark <jean...@gmail.com> wrote:
> Hello.
>
> I'm currently trying to achieve editable site-wide setting for my website
> with Configure::write and read.
>
> My db had id, name and value (ex: 1, site.name, My site)
>
> I have this function in AppController :
>
> function fetchSettings(){
>        //Loading model on the fly
>        $this->loadModel('Setting');
>        //Fetching All params
>        $this->set('settings', $this->Setting->find('all'));
>        $name = $setting['Setting']['name'];
>        $value = $setting['Setting']['value'];
>        foreach($settings as $setting){
>           Configure::write("$name", "$value");
>        }
>     }
>
> This is my setting Model :
>
> <?php
> class Setting extends AppModel {
>     public $validate = array(
>         'name' => array(
>             'rule' => 'notEmpty'
>         ),
>         'value' => array(
>             'rule' => 'notEmpty'
>         )
>     );
>
> }
>
> And in my view I use :
>
> Configure::read('site.name')
>
> But it does'nt work... any idea ?

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