我有表学期,学科和联合表学期七学期。我想在DisciplinesController中使用semester_id作为参数创建一个动作索引,该参数使用paginate列出属于学期的学科,参数中传递了id。我试过这个:
public function index($semester_id)
{
$options = ['semester_id' => $semester_id];
$this->paginate = ['conditions' => $options];
$this->set('disciplines', $this->paginate($this->Disciplines));
$this->set('_serialize', ['disciplines']);
}
答案 0 :(得分:16)
您必须使用使用匹配或联接的查询才能过滤非1:1
/ n-1
个关联。
您可以通过将查询直接传递给paginate()
方法
// ...
$this->set('disciplines', $this->paginate(
$this->Disciplines
->find()
->matching('Semesters', function(\Cake\ORM\Query $q) use ($semester_id) {
return $q->where([
'Semesters.id' => $semester_id
]);
})
->group(['Disciplines.id'])
));
// ...
或使用自定义查找程序。
// ...
$this->paginate = [
'finder' => [
'semesters' => [
'semester_id' => $semester_id
]
]
];
$this->set('disciplines', $this->paginate($this->Disciplines));
// ...
// DisciplinesTable
public function findSemesters(\Cake\ORM\Query $query, array $options)
{
$query
->matching('Semesters', function(\Cake\ORM\Query $q) use ($options) {
return $q->where([
'Semesters.id' => $options['semester_id']
]);
})
->group(['Disciplines.id']);
return $query;
}
另见