【发布时间】:2014-03-05 08:00:44
【问题描述】:
在 CakePHPs 的博客教程中会有一个帖子通过以下操作保存:
public function add()
{
if ($this->request->is('post'))
{
$this->Post->create();
if ($this->Post->save($this->request->data))
{
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
我不太明白 Cookbook 中描述的$this->Post->create(); 的目的:
[...]它重置模型状态以保存新信息。它不是 实际上在数据库中创建了一条记录,但清除了 Model::$id [...]
(发现于Cookbook 2.x)
如果 create(); 不清除 Model::$id 会发生什么?
【问题讨论】:
-
我不明白这个问题
What will happen, if Model::$id will not be cleared by create();?- 你是问它是否会被清除? (是的,会的) -
@AD78six:感谢您的评论。当我删除行
$this->Post->create();时,我的帖子也会被保存。那我为什么不把它排除在外呢? -
它只是确保模型处于干净状态。你可以忽略它,但保持它是一个好习惯。
-
@AD7six:我可以想象,在某些情况下你必须清理模型。谢谢。
标签: cakephp cakephp-2.3