Guns
Guns

Reputation: 2728

CakePHP Model Name as Variable

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!

Upvotes: 0

Views: 1375

Answers (2)

zzlalani
zzlalani

Reputation: 24344

It should work this way

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

no need to use { } curly braces

Upvotes: 1

Moyed Ansari
Moyed Ansari

Reputation: 8461

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

Upvotes: 1

Related Questions