I am using CakePHP 2. We can access a Model named 'Setting' as $this->Setting->find('all') however, how could i make the 'Setting' value a Variable. Something like

$modelName = 'Setting';
$data = $this->{$modelName}->find('all');

Any help is appreciated!

有帮助吗?

解决方案

It should work this way

$modelName = 'Setting';
$data = $this->$modelName->find('all');

no need to use { } curly braces

其他提示

I use to initiate my main model like this

$modelClass = $this->Mymodel;

And I use model functions like this

$modelClass->id  = $id;
$modelClass->save($this->request->data);
$this->request->data = $modelClass->find('first',array('conditions'=>array('id'=>$id)));

I hope that will work for you

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top