【问题标题】:cakephp3- Unknown column in where clausecakephp3-where子句中的未知列
【发布时间】:2017-06-14 11:24:18
【问题描述】:

下面是联想

每所学校都有很多学生。 每所学校都属于一个学区

$this->table('school');

$this->belongsTo('Districts', [
    'foreignKey' => 'district_id'
]);
$this->hasMany('Students', [
    'foreignKey' => 'school_id'
]);

假设有 4 个区。 (地区.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 的新手。

【问题讨论】:

    标签: php mysql cakephp-3.0 cakephp-3.x


    【解决方案1】:

    您应该传递有效的 id。

    试试:

    id
    

    district_id
    

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 1970-01-01
      • 2021-09-19
      • 2020-06-28
      • 2021-07-02
      • 2019-08-14
      • 2013-07-18
      • 2016-10-04
      相关资源
      最近更新 更多