1

我很长时间尝试使用 CakePHP 获得两个表的简单列表菜单关系,但它不起作用。当我能够使用脚手架时,列表菜单可以工作(它是否表明这种关系是正确的?)但是我的代码不是。

我的 url (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');
}

分类.php

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

2 回答 2

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

错了,你可以放

$this->Post->Categoria->find()
于 2012-10-30T04:19:47.327 回答
1

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

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

于 2012-10-30T00:48:12.537 回答