2011-02-08 70 views
0

我已經完成了一些搜索,但找不到任何與我的方案相關的足夠/相關的工作。我有:CakePHP分頁結果與HABTM鏈接的另一個表上的條件

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

我想從我的Job控制器做一個paginate()與上User.id的條件,因爲我確實需要從當前用戶獲取-and paginate-的所有作業。

如果您確實提供了另一主題/網站的鏈接,請提供相關說明以及如何將其應用於我的案例。

乾杯,
尼古拉斯。

+0

你能描述一下你的條件想這樣做,也說明哪些字段的結果集應包含 – OldWest 2011-02-09 02:27:45

回答

1

確保您的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(); 
相關問題