如何在 Yii2 模型搜索中设置条件为空?



我想设置显示title_recievednull的所有车辆的条件。

->andFilterWhere(['=', 'tr.title_recieved', null])
->andFilterWhere(['is', 'tr.title_recieved', null])
->andFilterWhere(['is', [ 'tr.title_recieved', null]])

我已经尝试了所有可用的选项,null条件适用于andWhere,但不适用于andFilterWhere

像这样在查询中使用和位置。

->andWhere(['tr.title_recieved' => null]);

试试这个:

->andFilterWhere('tr.title_recieved is NULL');

**根据 yii2 doc andFilterWhere() 向现有条件添加一个额外的 WHERE 条件,但忽略空操作数。

新条件和现有条件将使用"AND"运算符连接。

此方法类似于 andWhere()。主要区别在于此方法将删除空查询操作数。因此,此方法最适合根据用户输入的筛选器值构建查询条件。 来自文档 http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#andFilterWhere()-detail **

可以这样做

$query->andFilterWhere(['IS', 'tr.title_recieved', new yiidbExpression('NULL')]);

最新更新