问题描述
我有表学期,学科和联合表Semesters_Disciplines。我想在DisciplinesController中创建一个具有semester_id作为参数的操作索引,该列表只包含分类,属于具有在参数中传递的id的学期。我尝试过:
公共函数索引($ semester_id)
{
$ options = ['semester_id '=> $ semester_id];
$ this-> paginate = ['conditions'=> $ options];
$ this-> set('disciplines',$ this-> paginate($ this-> Disciplines));
$ this-> set('_ serialize',['disciplines']);
}
使用匹配或联接可以筛选非 1:1
/ n-1
关联的查询。 / p>
您可以直接将查询传递到 paginate()
方法
// ...
$ this-> set('disciplines',$ this-> paginate(
$ this - >学科
- > find()
- > match('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
- > match('Semesters',function(\Cake\ORM\Query $ q)use($ options){
return $ q-> where([
'Semesters.id'=> ; $ options ['semester_id']
]);
})
- > group(['Disciplines.id']);
return $ query;
}
另请参阅
- a href =http://book.cakephp.org/3.0/en/orm/query-builder.html#filtering-by-associated-data> Cookbook> QueryBuilder>按关联数据过滤
I have the tables Semesters, Disciplines and a jointTable Semesters_Disciplines. I want to create a action index in DisciplinesController with a semester_id as parameter, which list with paginate just the disciplines what belongs to the semester with the id passed in the parameter. I tried this:
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']);
}
You'll have to use a query that uses matching or joins to be able to filter on non 1:1
/n-1
associations.
You can do so by either passing a query directly to the paginate()
method
// ...
$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'])
));
// ...
or by using a custom finder.
// ...
$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;
}
See also
- Cookbook > Pagination > Using Controller::paginate()
- Cookbook > Retrieving Data and Result Sets > Custom Finder Methods
- Cookbook > QueryBuilder > Filtering by Associated Data
- Cookbook > QueryBuilder > Adding Joins
这篇关于分页条件为belongsToMany CakePHP 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!