2017-05-04 77 views
1

Helo,有人可以解釋我爲什麼會出現此錯誤嗎?我正在做的是我試圖根據id's篩選我的GridView,但它顯示此錯誤消息。我試圖搜索相關信息,但找不到有用的信息。SQLSTATE [23000]:完整性約束違規:1052 where子句中的列'id'不明確Yii2

這裏是我的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語句...謝謝您的幫助

回答

2

它should'nt工作,事業您可以加入item表格,該表格可以有另外的id列。更改此:

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

到:

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

另一種方法是使用別名表。

+0

恩,謝謝你,Yupik .. :)它工作 – devorye

相關問題