已解决,使用 withCount 关联统计报错:SQLSTATE[42S22]: Column not found: 1054 Unknown column
环境:本地 windows + 小皮 phpstudy
框架:基于 Thinkphp5.0 的 Fastadmin
PHP 版本:7.4.3
场景及代码描述:
我在开发一个外卖订单分析系统,有一个需求是对骑手订单量进行一个排行,表结构大致如下:
`knight` 骑手表:id、knight_local_code...
`orders` 订单表:id、knight_local_code...
骑手表与订单表是一对多的关系,我需要查询骑手数据并关联订单表的数据,代码如下:
knight模型:
public function orders()
{
return $this->hasMany('app\admin\model\Orders', 'knight_local_code', 'knight_local_code');
}
knight控制器:
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['team', 'type'])
->withCount('orders')
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('team')->visible(['name']);
$row->getRelation('type')->visible(['name']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
问题描述:
我使用 withCount
进行关联统计查询,会报以下错,但根据错误描述,实际上fa_knight表中的knight_local_code字段是存在的
:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fa_knight.knight_local_code' in 'where clause'
换个思路
于是为了排查是不是未知列的错误,我换了一种方式进行查询:
knight模型代码不变,我将knight控制器的代码修改成with关联,先不使用withcount,然后在foreach里面去count()查询订单数量,此时查询结果是正常的:
knight控制器:
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['team', 'type', 'orders'])
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('team')->visible(['name']);
$row->getRelation('type')->visible(['name']);
$row['orders_count'] = count($row->orders);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
请各位大佬帮忙看下是什么原因,十分感谢!
推荐文章: