Cakephp博客教程-控制器添加()操作

Cakephp博客教程-控制器添加()操作,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,在cakephp博客教程中的Add PostAdd()action部分 我不明白这是什么$this->Post->create()是的,我试图删除那行代码,但它仍然可以正常工作。那行代码是做什么的 public function add() { if ($this->request->is('post')) { $this->Post->create(); if ($this->Post->save($this->

在cakephp博客教程中的Add Post
Add()
action部分

我不明白这是什么
$this->Post->create()是的,我试图删除那行代码,但它仍然可以正常工作。那行代码是做什么的

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

它会创建该模型的一个新实例,然后在数据库表中创建一条新记录

添加数据时无需调用,但通常建议调用,因为如果您以前引用过任何其他记录,则在保存时会覆盖该数据。调用create可以确保您有一个新的slate来设置数据

保存数据的常规过程:

  • 创造
  • 设置($your_data)
  • 拯救