Cakephp: Access data in add function
I'm using the CakePHP blog example:
function add() {
if (!empty($this->data)) {
if ($this->Post->saveAll($this->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
}}
I want to modify it in a way that the user is redirected to the post after adding it:
But this doesn't work:
$this->redirect(array('action' => 'view', $this->Post->id)));
开发者_开发技巧
What is the correct way to read the model's data after creation?
As metrobalderas says, break up the save.
if ($this->Post->save($this->data))
{
unset($this->data['Post']; // So that we don't add another
$this->Post->saveAll($this->data);
$this->redirect(array('action' => 'view', $this->Post->id)));
}
精彩评论