CakePHP; how to make find conditions dependent upon foreign key of $id parameter
I'm new to programming so please forgive me if this is a noob question!
I'm using Cake to build a blog, with the models users and entries.
I have a View page for the blog entries and I would like to add a list with all the other entries that its creator has written to it.
So what I need is for the find function in my entries controller to display only the entries with the same number as the foreign key "user_id" of the currently viewed entry.
Currently, in the entries controller I have added the following:
$this->set('entries',
开发者_如何转开发 $this->Entry->find('all', array(
'conditions'=>array('Entry.user_id' => $id)
)
)
);
So it takes the parameter of the view action, rather than its foreign key, which isn't quite what I want.
But the problem is that I just can't think of how to get the foreign key...
Hope you can help me out with that, thanks.
function view($id = null){
if(!$id)$this->redirect(/*somewhere*/);
$entry = $this->Entry->read(null,$id);
$entries = $this->Entry->find('all', array(
'conditions'=>array('Entry.user_id' => $entry['Entry']['user_id')
)
);
$this->set(compact('entry','entries'));
}
You will need a certain degree of familiarity of Cake (don't just blindly copy the code). Hopefully the above code will point you to the right direction.
精彩评论