Question

Hello All I checked all the posts regarding this ERROR. But my context is little different. Please have a look.

My Model:-

class Post extends AppModel {


    function add($data){
        if (!empty($data)) {
    $this->create();
    if($this->save($data)) {
        return true ; 

        }
    }
 }
    //For Add Action
   function fetchdata() {
        return  $this->find('all',array( 'order' => array('id DESC') ) );   

    }
   //For Edit Action
   function fetch() {
        return  $this->Posts->find('all',array( 'order' => array('id DESC')) ); 

    }

    function getdata($id) {
        return  $this->Posts->find('all',array( 'conditions' => array('id'=>$id)) );    

    }
    //For Index Action
    function retrievedata($id) {
        return  $this->Posts->find('all',array( 'conditions' => array('id'=>$id)) );    

    }

}

Here I am using the EDIT FUNCTION

//For Edit Action
   function fetch() {
        return  $this->Posts->find('all',array( 'order' => array('id DESC')) ); 

    }

My Controller:-

class PostsController extends AppController {

public $uses = array('Post');

public function index(){

    if(isset($this->request->params['pass'][0])){

$this->set('showdata',$this->Post->retrievedata($this->request->params['pass'][0]));
     }

}   

public function add()
 {

         if(!empty($this->request->data)){
        if($this->Post->add($this->request->data)==true){
            $this->redirect(array('action'=>'index'));
         }

        }
    }

public function edit(){

    $this->set('post',$this->Post->fetch());
    if(isset($this->request->params['pass'][0])){

$this->set('showdata',$this->Post->getdata($this->request->params['pass'][0]));
    }
}  

}

The Edit Function is what I am dealing with

public function edit(){

    $this->set('post',$this->Post->fetch());
    if(isset($this->request->params['pass'][0])){

   $this->set('showdata',$this->Post->getdata($this->request->params['pass'][0]));
    }
}  

Now the ERROR

Fatal Error

Error: Call to a member function find() on a non-object
File: C:\wamp\www\blogproject\app\Model\Post.php
Line: 51
Was it helpful?

Solution

Your model name is Post not Posts so change Posts->find() to Post->find(). Also for example in Post->fetch() your are in Post model scope so use $this->find() not $this->Post->find().

Read about cake conventions. Model names are singular.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top