cakephp3- where 子句中的未知列
cakephp3- Unknown column in where clause
以下是协会
每个学校都有很多学生。
每所学校属于一个地区
$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 分的学生展示。
这是我为上面写的finder函数
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
以下是协会
每个学校都有很多学生。 每所学校属于一个地区
$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 分的学生展示。 这是我为上面写的finder函数
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