简体   繁体   English

CakePHP在与HABTM链接的另一个表上有条件的情况下分页结果

[英]CakePHP paginate results with a condition on another table linked with HABTM

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. 我想通过Job控制器在User.id上添加条件来执行paginate() ,因为我确实需要从当前用户获取并分页所有作业。

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. 尼古拉斯

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. 确保您的HABTM协会是设置正确( cake bake所期望的连接表jobs_users而非users_jobs (参见: CakePHP的-HABTM )如果是这样,改变在下面的例子来JobsUser的引用UsersJob匹配您所描述的布局。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM