开发者

Cakephp: Access data in add function

开发者 https://www.devze.com 2023-02-01 17:12 出处:网络
I\'m using the CakePHP blog example: function add() { if (!empty($this->data)) { if ($this->Post->saveAll($this->data)) {

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)));
}        
0

精彩评论

暂无评论...
验证码 换一张
取 消