thinkhp6 withJoin和withSum同时使用报错
代码如下:
public function examPaper(){
return $this->hasOne(ExamPaper::class, "ep_id", "es_epid")->field("ep_id,ep_name");
}
public function user(){
return $this->hasOne(User::class, "us_id", "es_uid")->field("us_id,us_name,us_number,us_unit,us_department");
}
public function examRecord(){
return $this->hasMany(ExamRecord::class, "er_esid", "es_id");
}
public static function getExamStatisticsPage(array $map=[], int $sort){
$webInfo = website(); //平台设置
$sort == 1 ? $order = "score Desc, es_id Desc" : $order = "score Asc, es_id Desc"; //排序
return ExamStatistics::field("es_id,es_epid,es_uid,es_examNum,es_electExamNum")
->withSum(["examRecord"=>"score"], "er_score")
->withJoin([
"examPaper"=>["ep_id", "ep_name"],
"user"=>["us_id","us_name","us_number","us_unit","us_department"],
])
->where($map)->order($order)->paginate(['list_rows'=>PageNum,'query'=>Request::param()])->each(function ($item) use ($webInfo){
$item["examNum"] = $item["es_examNum"] + $item["es_electExamNum"]; //已考次数
$item["notExamNum"] = $webInfo["examNum"] - $item["examNum"]; //未考次数
return $item;
});
}
报错信息:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘tb_exam_statistics.es_id’ in ‘where clause’
去掉withJoin或者withSum其中一个就可以正常显示数据,有没有大神解答一下小弟的疑惑
withSum
第三个参数填false
试试