以下是关联
每所学校都有很多学生。每所学校属于一个学区
$this->table('school');
$this->belongsTo('Districts', [
'foreignKey' => 'district_id'
]);
$this->hasMany('Students', [
'foreignKey' => 'school_id'
]);
假设有 4 个区。(Districts.id = 1,2,3,4(在学校模型中,我正在尝试获取所有学生的列表。但如果学区是 1 或 2,我想只显示那些得分超过 50 分的学生。对于其他地区,我试图展示得分低于 35 分的学生。这是我为上面提到的查找器功能编写的查找器功能。
public function findStudentsList(Query $query, array $options)
{
$query->select(['id','name','modified','created'])
->contain([
'Districts'=> function ($query) {
return $query->select(['id','name']);
},
'Students' => function ($query) {
return $query->where([
'CASE
WHEN Districts.id=1 THEN Students.marks >50
WHEN Districts.id=2 THEN Students.marks >50
ELSE Students.marks < 35
END']);
}
]);
}
但是当我执行此操作时,我收到以下错误。
Column not found: Unknown column 'Districts.id' in where clause
这是正确的方法吗?我是 CakePHP3 的新手。
您应该传递有效的 id。
尝试:
id
自
district_id