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'),
]);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With