【问题标题】:CakePhp- save method while saving data from a FormCakePhp- 从表单保存数据时的保存方法
【发布时间】:2023-03-28 02:54:02
【问题描述】:

我在保存表单数据时遇到了这个错误。

“致命错误:在 H:\wamp\www\onlineblog\app\Controller\PostsController.php 第 23 行调用非对象的成员函数 save()”

这里是 add 方法 PostsController。

public  function  add(){
   if($this->request->is('post')){
      $this->loadModel('Post');
      if($this->set->Post->save($this->request->data)){
        $this->Session->setFlash("Post added successfully");
          $this->redirect(array('action'=>'hello_cake'));
      }
       else{
           $this->Session->setFlash("Post Can't be added");
       }
   }
}

这是添加视图

<h2>Add a Post</h2>
<?php
echo $this->Form->create('Post',array('action'=>'add'));
echo $this->Form->input('heading');
echo $this->Form->input('body');
echo $this->Form->end('Create a Post');

?>

【问题讨论】:

  • 您使用的是哪个版本的 cakephp?

标签: php forms cakephp


【解决方案1】:

使用这个:

$this->Post->save(

而不是这个:

$this->set->Post->save(

【讨论】:

    【解决方案2】:

    试试这个,我认为会奏效。

    public  function  add(){
       if($this->request->is('post')){
          $this->Post->create();
          if($this->Post->save($this->request->data)){
              $this->Session->setFlash("Post added successfully");
              $this->redirect(array('action'=>'hello_cake'));
          }
           else{
               $this->Session->setFlash("Post Can't be added");
           }
       }
    }
    

    【讨论】:

    • 如果您只是转储代码 - 您所做的更改并不总是很明显。 总是提供解释。
    猜你喜欢
    • 2015-08-25
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2013-09-24
    • 2012-05-24
    • 1970-01-01
    相关资源
    最近更新 更多