YIi2 - 比较 searchModel 中的两个字段 [英] YIi2 - comparing two field in searchModel

查看:34
本文介绍了YIi2 - 比较 searchModel 中的两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 TPurchasing 的表,其中包含有关商店购买记录的数据.

I have a table called TPurchasing which contain data about a store's purchasing record.

购买

  • id |连载
  • transaction_time |没有时区的时间戳
  • 供应商 |字符变化(200)
  • 总计 |双精度
  • 付费|双精度

通过在 yii2 中使用 gii 功能,我设法从CRUD Generator"菜单生成了一个名为 TPurchasingSearch 的模型.在TPurchasingSearch中,有一个名为search()的函数,供视图文件中的GridView使用.

By using gii feature in yii2, I manage to generate a model called TPurchasingSearch from "CRUD Generator" menu. In the TPurchasingSearch, there is a function called search() which is used by the GridView in the view file.

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;
}

现在我正在尝试显示仍未完全付清的记录,这意味着已付"列小于总计"列.如何在搜索功能中进行这种比较?上面的代码给了我一个错误:

Right now I'm trying to display records that is still not totally paid off, which means the "paid" column is smaller than "total" column. How can I do this comparison in the search function? The above code gave me an error:

invalid input syntax for type double precision: "paid"

这部分

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

因为它试图将总列与字符串已付"进行比较.我可能错过了,但我似乎无法在 Yii2 文档中找到相关答案.

since it tried to compare total column with string "paid". I might missed it, but I can't seem to find the related answer in Yii2 documentation.

推荐答案

这里不需要使用 andFilterWhere(),你可以简单地尝试:

No need to use andFilterWhere() here, you could simply try :

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

这篇关于YIi2 - 比较 searchModel 中的两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆