【问题标题】:Cakephp Blog Tutorial - Controller Add() ActionCakephp 博客教程 - 控制器 Add() 动作
【发布时间】:2013-12-14 02:06:37
【问题描述】:

在 cakephp 博客教程中添加帖子add() 操作部分

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

我不明白这个$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.'));
    }
}   

【问题讨论】:

    标签: php cakephp cakephp-2.0


    【解决方案1】:

    它会创建该模型的一个新实例,进而在您的数据库表中创建一个新记录。

    添加数据时不需要调用,但通常建议这样做,因为如果您之前引用了任何其他记录,它会在您保存时覆盖该数据。调用 create 可确保您有一个新的设置来设置数据。

    保存数据的常规流程:

    • 创建
    • 设置($your_data)
    • 保存

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多