【问题标题】:In Yii2 how to perform a sql function inside andFilterWhere() method? or What is the alternative method?在 Yii2 中如何在 andFilterWhere() 方法中执行 sql 函数?或替代方法是什么?
【发布时间】:2016-06-25 11:06:28
【问题描述】:
->andFilterWhere(['=>',"invoiceDate - dueDate",$this->overDueLimit])
我需要这个过滤器在 gridview 中提取过期发票。 “invoiceDate - dueDate”应该是一个返回数字的 sql 函数。但是 Yii2 把它当作一个字符串(作为一个字段名)。
如何以正确的方式执行相同的任务?谢谢
【问题讨论】:
标签:
mysql
database
activerecord
yii2
query-builder
【解决方案1】:
感谢 Scais Edge。我根据您的建议找到了解决方案。我在搜索方法中添加了以下几行,它解决了我的问题。
if(!empty($this->overDue))
{
$terms=MyFunc::validate($this->overDue);// sanitize and get number
$query->andFilterWhere(['>=',"DATEDIFF(CURDATE(),`dueDate`)",$terms])
->andFilterWhere(['=', 'status', self::$pending]);
}
【解决方案2】:
似乎 filterWhere 不允许计算值,因此您可以改用 andWhere() 来接受文字 where 条件。所以你可以为你需要的查询创建你的where条件..在这种情况下你必须检查$this->overDueLimit的值是否设置..
if ( isset($this->overDueLimit ) ) {
$query->andWhere("invoiceDate - dueDate => " . $this->overDueLimit);
}
检查是否对 var $this->overDueLimit 进行了适当的清理