SQLSTATE[23000]:完整性约束冲突:1052列';id';在where子句中是不明确的Yii2
Helo有人能解释一下为什么我会犯这个错误吗?我所做的是,我试图根据SQLSTATE[23000]:完整性约束冲突:1052列';id';在where子句中是不明确的Yii2,yii2,Yii2,Helo有人能解释一下为什么我会犯这个错误吗?我所做的是,我试图根据id过滤我的GridView,但它会显示此错误消息。我试图搜索相关信息,但找不到有用的信息 这是我的搜索功能: public function search($params) { $query = Sale::find(); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); $query
id
过滤我的GridView
,但它会显示此错误消息。我试图搜索相关信息,但找不到有用的信息
这是我的搜索功能:
public function search($params)
{
$query = Sale::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->joinWith('item');
$dataProvider->setSort([
'attributes' => [
'id',
'customer_name',
'customer_surname',
'customer_phone',
'customer_email',
'code',
'comment',
'sign',
'price' => [
'asc' =>['item.price' => SORT_ASC],
'desc' =>['item.price' => SORT_DESC],
],
'item_id' => [
'asc' =>['item.name' => SORT_ASC],
'desc' =>['item.name' => SORT_DESC],
]
]
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'customer_name', $this->customer_name])
->andFilterWhere(['like', 'item.name', $this->item_id])
->andFilterWhere(['like','sign', $this->sign])
->andFilterWhere(['like', 'customer_surname', $this->customer_surname])
->andFilterWhere(['like', 'customer_phone', $this->customer_phone])
->andFilterWhere(['like', 'customer_email', $this->customer_email])
->andFilterWhere(['like', 'code', $this->code])
->andFilterWhere(['like', 'comment', $this->comment]);
return $dataProvider;
}
它应该可以工作,因为id
已添加到setSort()
和filter
语句中。。。感谢您提供的任何帮助它应该不起作用,因为您加入了项
表,该表可能还有另一个id
列。更改此项:
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
致:
另一种方法是为表使用别名。使用别名更难实现,尤其是在使用复杂的GridView链接等的情况下。以上是最简洁的方法。
// grid filtering conditions
$query->andFilterWhere([
'sale.id' => $this->id,
]);