2017-09-05 147 views
0

我有一個名爲TPurchasing的表,其中包含有關商店採購記錄的數據。YIi2 - 在searchModel中比較兩個字段

TPurchasing

  • ID | serial
  • transaction_time |不帶時區的時間戳
  • 供應商|字符變化(200)
  • total |雙精度
  • 已付|雙精度

通過使用yii2中的gii特性,我設法從「CRUD Generator」菜單生成一個名爲TPurchasingSearch的模型。在TPurchasingSearch中,有一個稱爲search()的函數,GridView在視圖文件中使用該函數。

public function search($params) 
{ 
    $query = TPurchasing::find(); 
    $dataProvider = new ActiveDataProvider([ 
     'pagination' => ['pageSize' => 20], 
     'query' => $query, 
     'sort' => ['defaultOrder' => ['transaction_time' => SORT_DESC]] 
    ]); 

    $this->load($params); 

    if (!$this->validate()) { 
     $query->where('0=1'); 
     return $dataProvider; 
    } 

    $query->andFilterWhere(['is_removed' => 0]); 
    $query->andFilterWhere(['like', 'supplier_name', $this->supplier_name]) 
     ->andFilterWhere(['like', 'payment_type', $this->payment_type]) 
     ->andFilterWhere(['>', 'total', 'paid']); 

    return $dataProvider; 
} 

現在我想顯示仍然沒有完全還清,這意味着「支付」一欄是比「總量」列較小的記錄。我如何在搜索功能中進行這種比較?上面的代碼給了我一個錯誤:

invalid input syntax for type double precision: "paid" 

在這部分

->andFilterWhere(['>', 'total', 'paid']); 

,因爲它試圖總量列與字符串「支付」進行比較。我可能會錯過它,但我似乎無法在Yii2文檔中找到相關答案。

回答

2

沒有必要在這裏使用andFilterWhere(),你可以簡單地嘗試:

$query->andWhere('total > paid'); 
+0

它的伎倆!從來沒有想過會這麼簡單。謝謝! – Switch88