Cakephp很多很多关系找到条件

时间:2016-03-28 11:16:59

标签: cakephp

我需要过滤帖子,以便获得仅与类别相关的帖子。我的网址将是../ posts / index / 4

型号:

Snapshot

我的索引代码如下:

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']);
}

0 个答案:

没有答案