CakePHP错误:在非对象上调用成员函数find()

时间:2014-04-23 06:09:45

标签: php html css cakephp fatal-error

Hello All 我查看了有关此错误的所有帖子。但我的背景略有不同。请看看。

我的模特: -

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

    }

}

我在这里使用编辑功能

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

    }

我的控制器: -

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

}

编辑功能是我正在处理的

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

现在是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

1 个答案:

答案 0 :(得分:1)

您的型号名称为“不是帖子”,因此请将Posts->find()更改为Post->find()。 另外,例如在Post->fetch()您的帖子模型范围内,请使用$this->find()而不是$ this-> Post-> find()。

了解cake conventions。型号名称是单数。