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]));
}
}
Fatal Error
Error: Call to a member function find() on a non-object
File: C:\wamp\www\blogproject\app\Model\Post.php
Line: 51
答案 0 :(得分:1)
您的型号名称为“不是帖子”,因此请将Posts->find()
更改为Post->find()
。
另外,例如在Post->fetch()
您的帖子模型范围内,请使用$this->find()
而不是$ this-> Post-> find()。
了解cake conventions。型号名称是单数。