cakePHP get a variable from each Model and Controller -
i got question. have db table settings (id, name). if read them db
$settings = $this->setting->find('list');
how can in appcontroller or access each controller , model?
hope can me.
thanks
explanation:
i assume you're looking below (obviously you'll want tweak per own application, - it's idea).
in app controller, it
- finds settings table
- repeats through each , puts each 1 "configure" variable
code:
/** * read settings db , populate them in constants */ function fetchsettings(){ $this->loadmodel('setting'); $settings = $this->setting->findall(); foreach($settings $settingsdata) { $value = $settingsdata['setting']['default_value']; //note: can't check !empty because values 0 (zero) if(isset($settingsdata['setting']['value']) && $settingsdata['setting']['value'] !== null && $settingsdata['setting']['value'] !== '') { $value = $settingsdata['setting']['value']; } configure::write($settingsdata['setting']['key'], $value); } }
then, can access them anywhere in app via configure::read('myvar');
a warning cakephp book configure variables. (i think they're fine use in case, - keep in mind):
cakephp’s configure class can used store , retrieve application or runtime specific values. careful, class allows store in it, use in other part of code: sure temptation break mvc pattern cakephp designed for. main goal of configure class keep centralized variables can shared between many objects. remember try live “convention on configuration” , won’t end breaking mvc structure we’ve set in place.
Comments
Post a Comment