问题描述
朋友们,我如何在下面(在 Yii2 Framewok 中)获取我的 DatePicker 组件,过滤 datetime
类型的字段?由于在组件中我只能指定 date
格式.
Friends, how do I get my DatePicker component below (in Yii2 Framewok), filter a field of type datetime
? Since in the component I can only specify the date
format.
_search.php 文件:
<?php
echo DatePicker::widget([
'model' => $model,
'attribute' => 'start_date',
'attribute2' => 'end_date',
'language' => 'pt',
'type' => DatePicker::TYPE_RANGE,
'separator' => 'até',
'options' => [
'placeholder' => '',
],
'pluginOptions' => [
'autoclose'=>true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd',
]
]);
?>
更新
public function search($params)
{
$query = Report::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'created' => SORT_DESC,
]
],
'pagination' => [
'pageSize' => 100,
],
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'created' => $this->created,
'updated' => $this->updated,
'closed' => $this->closed,
'user_id' => $this->user_id,
'status_id' => $this->status_id,
'location_id' => $this->location_id,
'typeperson_id' => $this->typeperson_id,
'field_cpfcnpj' => $this->field_cpfcnpj,
]);
$query->andFilterWhere(['between', 'created', $this->start_date, $this->end_date]);
$query->andFilterWhere(['between', 'closed', $this->start_closed, $this->end_closed]);
return $dataProvider;
}
推荐答案
如果我理解正确,您想使用日期范围提交表单,该日期范围应使用给定范围过滤记录.
If i understand correctly, you want to submit form using the date range which should filter the records using the given range.
看看您的 search()
方法,您似乎在搜索模型中声明了 2 个公共属性/字段,名称分别为 start_date
和 end_date
与 DatePicker
一起使用,并且您试图将范围与 created
列进行比较.
Looking at you search()
method it looks like you have declared 2 public properties/fields in the search model with the name start_date
and end_date
which you are using with the DatePicker
and you are trying to compare the range with the column created
.
您需要执行以下操作才能正确过滤记录
You need to do the following in order to filter the records correctly
确保以下内容
start_date
和end_date
在ReportSearch
模型的safe
规则中声明.
The
start_date
andend_date
are declared inside thesafe
rules for theReportSearch
model.
需要使用\yii\db\Expression
将列中的日期转换成想要的格式,并使用php:date
格式化给定的日期范围,即 start_date
和 end_date
.
You need to use the \yii\db\Expression
to convert the date in the column to the desired format, and use the php:date
to format the given date ranges i.e start_date
and end_date
.
在 search()
方法中返回 $dataProvider
之前添加以下内容
Add the following before you return the $dataProvider
in the search()
method
if ($this->start_date !== null && $this->end_date !== null) {
$query->andFilterWhere(
[
'BETWEEN',
new Expression(
'DATE_FORMAT(created,"%Y/%m/%d")'
),
date("Y/m/d", strtotime($this->start_date)),
date("Y/m/d", strtotime($this->end_date)),
]
);
}
注意:如果您将 created
列保存为 timestamp
,那么您需要使用 FROM_UNIXTIME
将字段名称包装在现有查询中,例如下面,否则如果列是 DATE
或 DATETIME
上面的将起作用.
Note: if you have the created
column saved as timestamp
then you need to wrap the field name in the existing query with FROM_UNIXTIME
like below, otherwise if the column is of DATE
or DATETIME
the above will work.
if ($this->start_date !== null && $this->end_date !== null) {
$query->andFilterWhere(
[
'BETWEEN',
new Expression(
'DATE_FORMAT(FROM_UNIXTIME(created),"%Y/%m/%d")'
),
date("Y/m/d", strtotime($this->start_date)),
date("Y/m/d", strtotime($this->end_date)),
]
);
}
您完整的search()
方法如下所示
Your complete search()
method will look like below
public function search($params)
{
$query = Report::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'created' => SORT_DESC,
]
],
'pagination' => [
'pageSize' => 100,
],
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'created' => $this->created,
'updated' => $this->updated,
'closed' => $this->closed,
'user_id' => $this->user_id,
'status_id' => $this->status_id,
'location_id' => $this->location_id,
'typeperson_id' => $this->typeperson_id,
'field_cpfcnpj' => $this->field_cpfcnpj,
]);
if ($this->start_date !== null && $this->end_date !== null) {
$query->andFilterWhere(
[
'BETWEEN',
new Expression(
'DATE_FORMAT(created_at,"%Y/%m/%d")'
),
date("Y/m/d", strtotime($this->start_date)),
date("Y/m/d", strtotime($this->end_date)),
]
);
}
$query->andFilterWhere(['between', 'closed', $this->start_closed, $this->end_closed]);
return $dataProvider;
}
这篇关于DatePicker 小部件过滤日期时间字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!