Reputation: 105
->andFilterWhere(['=>',"invoiceDate - dueDate",$this->overDueLimit])
i need this filter to extract overDue invoices in gridview. "invoiceDate - dueDate" should be a sql function which returns a number. but Yii2 takes this as a string(as a field name). How to perform the same task in a correct way? Thanks
Upvotes: 0
Views: 1141
Reputation: 105
Thanks Scais Edge.I found the solution based on your suggestion. I added the following lines to the search method and it solved my problem.
if(!empty($this->overDue))
{
$terms=MyFunc::validate($this->overDue);// sanitize and get number
$query->andFilterWhere(['>=',"DATEDIFF(CURDATE(),`dueDate`)",$terms])
->andFilterWhere(['=', 'status', self::$pending]);
}
Upvotes: 1
Reputation: 133370
seems the filterWhere don't allow calculated value so you can use instead andWhere() that admit literal where condition. So you can create your where condition for the query you need .. in this case you must check if the value of $this->overDueLimit
is setted ..
if ( isset($this->overDueLimit ) ) {
$query->andWhere("invoiceDate - dueDate => " . $this->overDueLimit);
}
check obviusly for a proper sanitaze of the var $this->overDueLimit
Upvotes: 0