簡體   English   中英

CakePhp-從表單保存數據時保存方法

[英]CakePhp- save method while saving data from a Form

從表單保存數據時遇到此錯誤。

“致命錯誤:在第23行的H:\\ wamp \\ www \\ onlineblog \\ app \\ Controller \\ PostsController.php中的非對象上調用成員函數save()”

這是add methos 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');

?>

用這個:

$this->Post->save(

而不是這個:

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

嘗試這個我認為這將有效。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM