【问题标题】:Cakephp: Access data in add functionCakephp:在添加功能中访问数据
【发布时间】:2010-12-24 20:25:13
【问题描述】:

我正在使用 CakePHP 博客示例:

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

我想修改它,让用户在添加后重定向到帖子:

但这不起作用:

  $this->redirect(array('action' => 'view', $this->Post->id)));    

创建后读取模型数据的正确方法是什么?

【问题讨论】:

  • 可能是因为您使用的是saveAll()。尝试使用save() 并找出$this->Post->id 中的内容

标签: cakephp


【解决方案1】:

正如 Metrobalderas 所说,打破存档。

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

【讨论】:

  • $this->saveAll($this->data);不起作用:调用未定义的方法 CarsController::saveAll()
  • 他的意思是:$this->Model->saveAll,在本例中为 $this->Post->saveAll(逻辑上^^)
  • 对不起。已更正(假设我们在 PostsController 中)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
相关资源
最近更新 更多