Cakephp中的多对多关系

时间:2016-07-10 12:19:18

标签: cakephp cakephp-2.0

我已经拥有徽章,用户和badges_users表。 我想在徽章控制器中构建一个list()来列出会话用户拥有的所有徽章。到目前为止,我得到了这个,我不知道如何继续。它必须与Paginator组件一起工作

public function list() {
    $this->Badge->recursive = 0;        
    $this->paginate['conditions'] = array('user_id' => $this->Auth->user('id'));            
    $this->Paginator->settings = $this->paginate;           
    $this->set('badges', $this->paginate());
}

1 个答案:

答案 0 :(得分:1)

user_id字段仅在badge_users表中可用,而不在徽章表中,因此您应该在badge_users控制器中构建list(),并在paginate设置中定义包含参数到徽章模型。

//in badges_users controller
public function list() {
    $this->BadgesUsers->recursive = 2;      
    $this->paginate['conditions'] = array('user_id' => $this->Auth->user('id'));    

//contain parameter
$this->paginate['contain'] = array('Badges');

    $this->Paginator->settings = $this->paginate;           
    $this->set('badges', $this->Paginator->paginate('BadgesUsers));
}