Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is andFilterWhere() condition operands purpose?

Tags:

yii2

I'm absolutely new to Yii2 framework. I'm trying to learn filtering, but somehow I don't understand it. I've looked in the documentation, but it didn't helped too. Could someone explain to me what this function does step by step?

public function filteringValues($query)
{   
        $query->andFilterWhere([
            '>',
            'table1.column1',
            date('Y-m-d H:i:s'),
        ]);
    }
}
like image 766
HarryFlopper Avatar asked Dec 23 '22 16:12

HarryFlopper


1 Answers

just like the doc points out, andFilterWhere applies a condition if the operands are not empty

in your particular case (since date('Y-m-d H:i:s') always returns a value),

$query->andFilterWhere(['>', 'table1.column1', date('Y-m-d H:i:s')]);

will be equivalent to

$query->andWhere(['>', 'table1.column1', date('Y-m-d H:i:s')]);

which is translated to sql to a condtion like

AND (`table1`.`column1`) > '2017-08-12 06:10:32'

a propper use case for andFilterWhere is when comparing with an optional value (received as a filter param).

$query->andFilterWhere(['>', 'table1.column1', $date]);

it's purpose is to not have to check whether $date is empty or not

$query->andFilterWhere([
    'table1.column1' => $param1,
    'table1.column2' => $param2,
    'table1.column3' => $param3,
]);

in this example, the query applies a condtion only for the params that are not empty, ignoring the extra ones

like image 57
csminb Avatar answered Mar 17 '23 03:03

csminb