0

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!

3
  • 4
    $data = $this->$modelName->find('all'); it should work Commented Oct 3, 2013 at 6:24
  • 1
    Thanks @zzlalani. This worked perfect. Can you post it as answer so that i can accept it. It might be helpful to our friends looking for such answers Commented Oct 3, 2013 at 7:00
  • 1
    although you have your answer already, but have a look stackoverflow.com/a/19153680/829533 Commented Oct 3, 2013 at 7:51

2 Answers 2

1

It should work this way

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

no need to use { } curly braces

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.