Arithmetic Filter

Hi All,

In Yii 1.1.x, I can easily filter the gridview with arithmetic operation, ">", "<", etc

How do we do this on Yii 2.0? It used to be just working.

In Yii 1.1, it was implemented using CDbCriteria::compare() which ignores an empty input and recognizes the leading ">", "<", ">=", "<=", "<>" and "=" in it.

Unfortunately, we don’t have the exact replacement of CDbCriteria::compare() in Yii 2.0. We have to do it on our own.

Try something like the following in the "search" method:

if ($this->some_field != "") {

    if(preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/', $this->some_field, $matches)) {

        $operator = $matches[1];

        $value = $matches[2];

        $query->andWhere([$operator, 'some_field', $value]);

    } else {

        $query->andWhere(['some_field' => $this->some_field]);

        // or, $query->andWhere(['like', 'some_field', $this->some_field]);



@softark, thank you for your prompt response. I think this is the best solution I can get.

Can you confirm that I cannot have rule for this field set as integer? I always got error when this field has rule set as integer in ModelSearch. i need to change it to safe to make it work.

Yeah, you are right. "<>123" is not an integer but a string.