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

时间:2017-05-04 12:45:26

标签: 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;
}

它应该有用,因为id已添加到setSort()filter语句中...感谢您的帮助

1 个答案:

答案 0 :(得分:5)

它应该工作,因为你加入item表,它可能有另一个id列。改变这个:

// grid filtering conditions
$query->andFilterWhere([
    'id' => $this->id,
]);

为:

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

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