开发者

CakePHP paginate results with a condition on another table linked with HABTM

开发者 https://www.devze.com 2023-02-09 02:25 出处:网络
I\'ve done some searching but I can\'t find anything relevant enough/working for my scenario. I\'ve got:

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, 开发者_Go百科please provide an explanation with it and how you would apply it to my case.

Cheers,

Nicolas.


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

精彩评论

暂无评论...
验证码 换一张
取 消