我想用 yii2 框架添加过滤条件,这个条件必须生成以下请求:
select count(*) from v_ressource
where
(
(
str_to_date('27/04/2016', '%d/%m/%Y')
between str_to_date(erDateDebut, '%d/%m/%Y')
and str_to_date(erDateFin, '%d/%m/%Y')
)
and erDateFin is not null
)
or
(
(
str_to_date('27/04/2016', '%d/%m/%Y')
between str_to_date(erDateDebut, '%d/%m/%Y')
and now()
)
and erDateFin is null
);
如您所见,“或”条件中有“和”条件。
我的 ModelSearch 中有以下代码:
$query
->andFilterWhere([
'between',
'str_to_date(\'' . $this->dateRecherche . '\', \'%d/%m/%Y %H:%i\')',
new Expression('str_to_date(erDateDebut, \'%d/%m/%Y %H:%i\')'),
new Expression('str_to_date(erDateFin, \'%d/%m/%Y %H:%i\')'),
])
->andFilterWhere([
'not', ['erDateFin' => null],
]);
$query
->orFilterWhere([
'between',
'str_to_date(\'' . $this->dateRecherche . '\', \'%d/%m/%Y %H:%i\')',
new Expression('str_to_date(erDateDebut, \'%d/%m/%Y %H:%i\')'),
new Expression('now()'),
])
->andFilterWhere([
'is', 'erDateFin', null,
]);
两者都是 null 和 is not null 条件不会出现在生成的请求中并且没有“嵌套”条件(以及 or 条件中的条件)
谢谢你的帮助