benone
benone

Reputation: 686

Many to Many Relationship in Cakephp

I am having badges, users and badges_users table already. I want to build a list() in badge controller to list out all the badges that the session user has. So far I got this and I don't know how to proceed. It has to work together with the Paginator component

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());
}

Upvotes: 1

Views: 284

Answers (1)

Yosi Azwan
Yosi Azwan

Reputation: 497

user_id field only available in badge_users table, not in badges table, so you should build list() in badge_users controller, and define contain parameter to badges model in paginate setting.

//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));
}

Upvotes: 1

Related Questions