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

时间:2012-10-29 23:08:32

标签: cakephp listbox

我很长时间尝试使用CakePHP获得一个简单的listmenu关系两个表,但它不起作用。当我能够使用脚手架列表菜单时(它是否显示关系是正确的?)但是我的代码没有。

我的网址(localhost / forum / posts / add)显示错误:

  

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

     

文件:C:\ wamp \ www \ forum \ app \ Controller \ PostsController.php

PostsController.php

class PostsController extends AppController {

public $name = 'Posts';

public function index() {

    $this->paginate = array('limit'=>3);
    $posts = $this->paginate('Post');
    $this->set(compact('posts'));
}
public function getCategorias(){
    $categorias = $this->Post->Categotia->find('list', array('fields' => array('id', 'categoria')));
    $this->set(compact('categorias'));
}

public function add(){
    if($this->data){
        if($this->Post->save($this->data))
            $this->Session->setFlash('Post adicionado com sucesso');
    $this->data = array();  
    }
    self::getCategorias();
}

public function edit($id = null){
    if($this->data){
        if($this->Post->save($this->data))
                $this->Session->setFlash('Post editado com sucesso');
        $this->redirect(array('controller'=>'Posts','action'=>'index'));    
    }else{
        $this->data = $this->Post->read(null,$id);

    }
    self::getCategorias();
}

public function delete($id = null){
    if($id){
        if($this->Post->delete($id))
            $this->Session->setFlash('Post Excluido com sucesso');
        $this->redirect(array('controller'=>'Posts','action'=>'index')) ;

    }
}

public function view($id = null){
    if($id){
        $this->Post->id = $id;
        $this->set('post', $this->Post->read());
    }

}

}

我的模特

Post.php:

class Post extends AppModel{
    public $name = 'Post';
    public $belongsTo = array('Categoria');
}

Categoria.php

class Categoria extends AppModel{
    public $name = 'Categoria';
    public $hasMany = array('Post');
}

2 个答案:

答案 0 :(得分:1)

也许你在这里拼错了分类:

  

$ categorias = $ this-> Post-> Categotia - > find('list',array('fields'=> array('id','categoria') ));

答案 1 :(得分:1)

 $this->Post->Categotia->find();  

错了,你可以把

$this->Post->Categoria->find()