我需要过滤帖子,以便获得仅与类别相关的帖子。我的网址将是../ posts / index / 4
型号:
我的索引代码如下:
public function index($cat_id = null)
{
$this->paginate = [
'contain' => ['Categories'],
'limit' => 2,
'conditions' =>['Posts.status' => 'publish'],
//'conditions' =>['PostsCategories.category_id' => '4'],
'order' => ['Posts.createdAt' => 'desc'],
'fields' => [
'Posts.id',
'Posts.title',
'Posts.excerpt',
'Posts.author',
'Posts.location',
'Posts.date',
'Posts.main_pic',
'Posts.hits'
],
];
$posts = $this->paginate($this->Posts);
$nextPage = $this->request->params['paging']['Posts']['nextPage'];
$this->set(compact('posts'));
$this->set(compact('nextPage'));
$this->set('_serialize', ['posts', 'nextPage']);
}