I have a model for User and a model for Notes.
User Model:
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Note'
);
}
User Controller:
public function notesview()
{
$allnotes = $this->User->Note->find('all', array('order'=>'Note.created DESC'));
}
View Element:
$allnotes = $this->requestAction(array('controller'=>'Users', 'action'=>'notesview'));
foreach($allnotes as $viewnotes):
{
echo $viewnotes['Note']['notes'];
echo "<br>";
echo $viewnotes['Note']['created'];
echo "<br>";
echo $viewnotes['User']['name'];
}
endforeach;
Now in the element you can see ” echo $viewnotes[‘User’][‘name’]; “. I want Name field from User model to be retrieved in this element but i get the following error:
Notice (8): Undefined index: User [APP\View\Elements\notes.ctp, line 39]
Do you have the belongsTo relationship set inside your Note model?
So, for example, you would have: