Ankit
Ankit

Reputation: 233

How can I read Database table value with the help of configure:: write & configure:: read in cakephp 2.x

We are migrating our website from cakephp 1.3 to cakephp 2.x, so I am facing a problem to use our current code element.

We have a table which has all default content which we used many place so we have created as a db table setting , so we are using it with configure:: read in our different view.

Our Setting Model:

 <?php
class Setting extends AppModel{
var $name   = 'Setting' ;

var $actsAs = array(
    'Multivalidatable',
    );

function getSetting(){
  $data = $this->find('list', array('fields'=>array('name', 'value')));

  if(!empty($data)){
    foreach($data as $key => $value)
        {
            Configure::write($key, $value); 
        }
  }

}


var $validationSets = array(
    // Start Of Admin Validation Set
            'setting' => array(
                    'value'=>array(
                        'notEmpty'=>array(
                        'rule'=>'notEmpty',
                        'message' => 'Value is required.'
                        )
                    )               
            )
        );
}
?>

So with this model we are setting some fields with configure::write, now we have used in view file like this

<?php echo Configure::read('Regpopup1.value');?>

but when I am migrating my code this logic is not working, please help me how can I use this code again with migration in cakephp 2.x

Upvotes: 0

Views: 314

Answers (1)

AlcoolGeek
AlcoolGeek

Reputation: 13

You can use something like that with Cakephp 2.x

App::uses('Controller', 'Controller');
class AppController extends Controller {

public function beforeFilter(){
    $this->fetchSettings(); // Don't forget this.
}   

public function fetchSettings(){
        $this->loadModel('Setting');
        $settings_array = $this->Setting->find('all');
        foreach($settings_array as $key=>$value){
            Configure::write($value['Setting']['key'], $value['Setting']['value']);
            }
    }
}

Upvotes: 1

Related Questions