Pergunta

I have faced this error while saving data from a form.

"Fatal error: Call to a member function save() on a non-object in H:\wamp\www\onlineblog\app\Controller\PostsController.php on line 23"

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

and here is the add view

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

?>
Foi útil?

Solução

Use this:

$this->Post->save(

Instead of this:

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

Outras dicas

Try this I think that will work.

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");
       }
   }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top