tp5 做关联查询,想要添加一个where条件,对id进行筛选,代码如下
$customWhere = ['id' => ['in',$websiteId]]; $list = $this->model ->with(['admin','category','language']) ->where($where) ->where($customWhere) ->order($sort, $order) ->limit($offset, $limit) ->select();
运行时报错:Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
原因:几个表都有id字段,不确定是差的哪个表对应的id
解决:在customWhere条件中,id前面加上模型名(model对应的名字是什么就写什么),修改后的代码如下
$customWhere = ['External_links.id' => ['in',$websiteId]]; $list = $this->model ->with(['admin','category','language']) ->where($where) ->where($customWhere) ->order($sort, $order) ->limit($offset, $limit) ->select();