0

I've done some searching but I can't find anything relevant enough/working for my scenario. I've got:

Jobs <--> HABTM (Users_jobs table) <--> Users

I would like to do a paginate() from my Job controller with a condition on the User.id, as I do need to fetch -and paginate- all the jobs from the current user.

If you do provide a link to another topic/site, please provide an explanation with it and how you would apply it to my case.

Cheers,
Nicolas.

Nicolas
  • 2,754
  • 6
  • 26
  • 41

1 Answers1

1

Make sure your HABTM associations are setup correctly (cake bake would expect a join table of jobs_users rather than users_jobs (see: Cakephp-HABTM) If they are, change the references to JobsUser in the example below to UsersJob to match the layout you described.

//In an action in jobs_controller.php

//Fake a HasOne association to the JobsUser model, setting $reset to false
$this->Job->bindModel(array(
    'hasOne' => array(
        'JobsUser'
    )
), false);

//Setup the paginate conditions, grouping on job.id
//Set $user_id to the user to filter results by (can also be an array of users)
$options = array(
    'group' => 'Job.id',
    'conditions' => array(
        'JobsUser.user.id' => $user_id
    )
);
$this->paginate = $options;
$users = $this->paginate();
wreality
  • 56
  • 4