0

我是 CakePHP 的新手。请帮助我编写一个函数来检索特定类别下的帖子,以便为我使用 CakePHP 构建的博客应用程序。

我的表结构:

帖子:id、post、body、created、category_id 类别:id、组

我还定义了:内部帖子模型 -var $belongsTo = 'Category'; 内部类别模型 -var $hasMany = 'Post';

4

2 回答 2

1

find() is the generic query method for Models in CakePHP.

An example would be:

$results = $this->Post->find('recursive' => -1, 'conditions' => array('Post.category_id' => 1));
debug($results);

There are many ways to achieve what you want. I encourage you to read the docs or working through the CakePHP Blog Tutorial.

于 2012-11-06T20:00:48.137 回答
0
$this->Post->find('all', array('conditions' => array('Post.category_id' => $category_id)));

其中 $category_id 是您要从数据库中检索结果的类别的 ID

希望这可以帮助

于 2012-11-07T09:42:39.510 回答