Reputation: 3
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');
?>
Upvotes: 0
Views: 99
Reputation: 4469
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");
}
}
}
Upvotes: -1
Reputation: 29121
Use this:
$this->Post->save(
Instead of this:
$this->set->Post->save(
Upvotes: 3