yii2 如何在另一个中创建和设置条件
yii2 how to create and condition in another one
我想用 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
);
如您所见,在 'or' 条件中有 'and' 个条件。
我的模型搜索中有以下代码:
$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,
]);
both of is null and is not null条件没有出现在生成的请求中并且没有"nested"条件(和or条件中的条件)
感谢您的帮助
andFilterWhere
和 orFilterWhere
不允许以这种方式嵌套,因为它们对查询对象进行操作 - 而不是对条件对象进行操作。您可以这样定义查询:
$query->where(
['and',
[
'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\')'),
],
[
'not', ['erDateFin' => null],
]
]);
$query->orWhere(
['and',
[
'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()'),
],
[
'is', 'erDateFin', null,
]
]);
您甚至可以将其全部放入一个 where
方法调用中:
$query->where(
['or',
['and',
[
'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\')'),
],
[
'not', ['erDateFin' => null],
]
],
['and',
[
'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()'),
],
[
'is', 'erDateFin', null,
]
]
]);
我用过 where method, not the filterWhere methods, because you probably don't want to remove empty operands from the query. More information about filtering can be found here. where 也记录了 and 和 or 运算符。
您可能已经知道,计数可以用
完成
$count = $query->count();
我想用 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
);
如您所见,在 'or' 条件中有 'and' 个条件。
我的模型搜索中有以下代码:
$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,
]);
both of is null and is not null条件没有出现在生成的请求中并且没有"nested"条件(和or条件中的条件)
感谢您的帮助
andFilterWhere
和 orFilterWhere
不允许以这种方式嵌套,因为它们对查询对象进行操作 - 而不是对条件对象进行操作。您可以这样定义查询:
$query->where(
['and',
[
'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\')'),
],
[
'not', ['erDateFin' => null],
]
]);
$query->orWhere(
['and',
[
'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()'),
],
[
'is', 'erDateFin', null,
]
]);
您甚至可以将其全部放入一个 where
方法调用中:
$query->where(
['or',
['and',
[
'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\')'),
],
[
'not', ['erDateFin' => null],
]
],
['and',
[
'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()'),
],
[
'is', 'erDateFin', null,
]
]
]);
我用过 where method, not the filterWhere methods, because you probably don't want to remove empty operands from the query. More information about filtering can be found here. where 也记录了 and 和 or 运算符。
您可能已经知道,计数可以用
完成$count = $query->count();