SQLSTATE [23000]:违反完整性约束:1052 where子句不明确的Yii2列'id'

dev*_*rye 2 yii2

Helo,有人可以向我解释为什么我会收到此错误吗?我正在做的是,我正在尝试GridView根据进行过滤id's,但它显示了此错误消息。我尝试搜索相关信息,但找不到有用的信息。

这是我的Search功能:

    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;
}
Run Code Online (Sandbox Code Playgroud)

它应该工作,因为id已将setSort()和添加到filter语句...谢谢您的帮助

Yup*_*pik 5

它不起作用,导致您加入了item表,该表可能还有另一id列。更改此:

// grid filtering conditions
$query->andFilterWhere([
    'id' => $this->id,
]);
Run Code Online (Sandbox Code Playgroud)

至:

// grid filtering conditions
$query->andFilterWhere([
    'sale.id' => $this->id,
]);
Run Code Online (Sandbox Code Playgroud)

另一种方法是对表使用别名。