我有一张table_date表。我希望有一个网格,可以按日期搜索该字段。截至目前,我要将整个日期用于搜索行。但我希望过滤学生一年。我在Yii2 GridView filter date by year
看到了类似的问题但无法找到他们在searchModel中提到的初始化$ year的位置。
请帮帮我。 阿伦
编辑:
class StudentsDetails extends \ yii \ db \ ActiveRecord {
/**
* @inheritdoc
*/
public static function tableName() {
return 'students_details';
}
public $year;
/**
* @inheritdoc
*/
public function rules() {
return [
[['admission_date', 'date_of_birth', 'slc_year', 'hsc_year','year'], 'safe'],
[['program', 'roll_no'], 'integer'],
[['address'], 'string'],
[['parent_annual_income', 'slc_total_mark', 'slc_percent', 'hsc_total_mark', 'hsc_percent'], 'number'],
[['regid', 'shift', 'landline', 'city'], 'string', 'max' => 15],
[['student_name', 'parent_name'], 'string', 'max' => 75],
[['gender', 'blood_group'], 'string', 'max' => 8],
[['mobile'], 'string', 'max' => 12],
[['district', 'province', 'country', 'parent_relation', 'parent_occupation', 'parent_contact', 'slc_board', 'hsc_board'], 'string', 'max' => 45],
[['slc_school', 'hsc_school'], 'string', 'max' => 120],
[['slc_symbol', 'hsc_symbol'], 'string', 'max' => 30],
[['section', 'semester'], 'string', 'max' => 2],
[['program'], 'exist', 'skipOnError' => true, 'targetClass' => Programs::className(), 'targetAttribute' => ['program' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels() {
return [
'id' => 'ID',
'regid' => 'Regid',
'admission_date' => 'Admission',
'program' => 'Program',
'shift' => 'Shift',
'student_name' => 'Name',
'date_of_birth' => 'Date Of Birth',
'gender' => 'Gender',
'blood_group' => 'Blood Group',
'landline' => 'Landline',
'mobile' => 'Mobile',
'address' => 'Address',
'city' => 'City',
'district' => 'District',
'province' => 'Province',
'country' => 'Country',
'parent_name' => 'Parent Name',
'parent_relation' => 'Parent Relation',
'parent_occupation' => 'Parent Occupation',
'parent_annual_income' => 'Parent Annual Income',
'parent_contact' => 'Parent Contact',
'slc_school' => 'Slc School',
'slc_board' => 'Slc Board',
'slc_symbol' => 'Slc Symbol',
'slc_year' => 'Slc Year',
'slc_total_mark' => 'Slc Total Mark',
'slc_percent' => 'Slc Percent',
'hsc_school' => 'Hsc School',
'hsc_board' => 'Hsc Board',
'hsc_symbol' => 'Hsc Symbol',
'hsc_year' => 'Hsc Year',
'hsc_total_mark' => 'Hsc Total Mark',
'hsc_percent' => 'Hsc Percent',
'roll_no' => 'Roll',
'section' => 'Section',
'semester' => 'Semester',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProgram0() {
return $this->hasOne(Programs::className(), ['id' => 'program']);
}
public static function getYearsList() {
$years = (new Query())->select('DISTINCT YEAR(`admission_date`) as years')->from('{{%students_details}}')->column();
return array_combine($years, $years);
}
搜索模型(StudentsDetailsSearch.php)
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\StudentsDetails;
/**
* StudentsDetailsSearch represents the model behind the search form about `app\models\StudentsDetails`.
*/
class StudentsDetailsSearch extends StudentsDetails {
/**
* @inheritdoc
*/
public function rules() {
return [
[['id', 'program', 'roll_no'], 'integer'],
[['regid', 'admission_date', 'shift', 'student_name', 'date_of_birth', 'gender', 'blood_group', 'landline', 'mobile', 'address', 'city', 'district', 'province', 'country', 'parent_name', 'parent_relation', 'parent_occupation', 'parent_contact', 'slc_school', 'slc_board', 'slc_symbol', 'slc_year', 'hsc_school', 'hsc_board', 'hsc_symbol', 'hsc_year', 'section', 'semester'], 'safe'],
[['parent_annual_income', 'slc_total_mark', 'slc_percent', 'hsc_total_mark', 'hsc_percent'], 'number'],
];
}
/**
* @inheritdoc
*/
public function scenarios() {
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params) {
$query = StudentsDetails::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'admission_date' => $this->admission_date,
'program' => $this->program,
'date_of_birth' => $this->date_of_birth,
'parent_annual_income' => $this->parent_annual_income,
'slc_year' => $this->slc_year,
'slc_total_mark' => $this->slc_total_mark,
'slc_percent' => $this->slc_percent,
'hsc_year' => $this->hsc_year,
'hsc_total_mark' => $this->hsc_total_mark,
'hsc_percent' => $this->hsc_percent,
'roll_no' => $this->roll_no,
]);
$query->andFilterWhere(['like', 'regid', $this->regid])
->andFilterWhere(['like', 'shift', $this->shift])
->andFilterWhere(['like', 'student_name', $this->student_name])
->andFilterWhere(['like', 'gender', $this->gender])
->andFilterWhere(['like', 'blood_group', $this->blood_group])
->andFilterWhere(['like', 'landline', $this->landline])
->andFilterWhere(['like', 'mobile', $this->mobile])
->andFilterWhere(['like', 'address', $this->address])
->andFilterWhere(['like', 'city', $this->city])
->andFilterWhere(['like', 'district', $this->district])
->andFilterWhere(['like', 'province', $this->province])
->andFilterWhere(['like', 'country', $this->country])
->andFilterWhere(['like', 'parent_name', $this->parent_name])
->andFilterWhere(['like', 'parent_relation', $this->parent_relation])
->andFilterWhere(['like', 'parent_occupation', $this->parent_occupation])
->andFilterWhere(['like', 'parent_contact', $this->parent_contact])
->andFilterWhere(['like', 'slc_school', $this->slc_school])
->andFilterWhere(['like', 'slc_board', $this->slc_board])
->andFilterWhere(['like', 'slc_symbol', $this->slc_symbol])
->andFilterWhere(['like', 'hsc_school', $this->hsc_school])
->andFilterWhere(['like', 'hsc_board', $this->hsc_board])
->andFilterWhere(['like', 'hsc_symbol', $this->hsc_symbol])
->andFilterWhere(['like', 'section', $this->section])
->andFilterWhere(['like', 'semester', $this->semester]);
// ->andFilterWhere(['like', 'year(admission_date)', $year]);
return $dataProvider;
}
GridView的
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
// 'regid',
[
'value' => 'regid',
'format' => 'raw',
'attribute' => 'regid',
'contentOptions' => ['class' => 'regid'],
],
[
'value' => 'admission_date',
'format' => 'raw',
'attribute' => 'admission_date',
//'filter' => \app\models\StudentsDetails::getYearsList(),
'contentOptions' => ['class' => 'admission_date'],
],
// 'admission_date',
//'program',
//'shift',
'student_name',
// 'date_of_birth',
// 'gender',
// 'blood_group',
// 'landline',
[
'value' => 'mobile',
'format' => 'raw',
'attribute' => 'mobile',
'contentOptions' => ['class' => 'mobile'],
],
'semester',
'section',
'roll_no',
// 'address:ntext',
// 'city',
// 'district',
// 'province',
// 'country',
// 'parent_name',
// 'parent_relation',
// 'parent_occupation',
// 'parent_annual_income',
// 'parent_contact',
// 'slc_school',
// 'slc_board',
// 'slc_symbol',
// 'slc_year',
// 'slc_total_mark',
// 'slc_percent',
// 'hsc_school',
// 'hsc_board',
// 'hsc_symbol',
// 'hsc_year',
// 'hsc_total_mark',
// 'hsc_percent',
['class' => 'yii\grid\ActionColumn', 'template' => '{view} {delete} {edit}',
'buttons' => [
'view' => function($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['studentview', 'sid' => $key]);
},
'delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['studentdelete', 'sid' => $key], [
'title' => \Yii::t('yii', 'Delete'),
'data-confirm' => \Yii::t('yii', 'Are you sure to delete this item?'),
'data-method' => 'post',
]);
},
'edit' => function($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['studentedit', 'sid' => $key]);
;
}
]],
//['class' => 'yii\grid\ActionColumn'],
],
]);
?>
答案 0 :(得分:0)
答案 1 :(得分:0)
在模型中删除$year
在gridView中添加(取消注释)年份的过滤器
[
'value' => 'admission_date',
'format' => 'raw',
'attribute' => 'admission_date',
'filter' => \app\models\StudentsDetails::getYearsList(),
'contentOptions' => ['class' => 'admission_date'],
],
并在searchModel中添加(取消注释)搜索过滤器andFilterWhere( 'year(admission_date) = year (' . $this->admission_date. ')' )
而不使用like(不是字符串是日期)并转换为年份相关列
$query->andFilterWhere(['like', 'regid', $this->regid])
->andFilterWhere(['like', 'shift', $this->shift])
->andFilterWhere(['like', 'student_name', $this->student_name])
->andFilterWhere(['like', 'gender', $this->gender])
->andFilterWhere(['like', 'blood_group', $this->blood_group])
->andFilterWhere(['like', 'landline', $this->landline])
->andFilterWhere(['like', 'mobile', $this->mobile])
->andFilterWhere(['like', 'address', $this->address])
->andFilterWhere(['like', 'city', $this->city])
->andFilterWhere(['like', 'district', $this->district])
->andFilterWhere(['like', 'province', $this->province])
->andFilterWhere(['like', 'country', $this->country])
->andFilterWhere(['like', 'parent_name', $this->parent_name])
->andFilterWhere(['like', 'parent_relation', $this->parent_relation])
->andFilterWhere(['like', 'parent_occupation', $this->parent_occupation])
->andFilterWhere(['like', 'parent_contact', $this->parent_contact])
->andFilterWhere(['like', 'slc_school', $this->slc_school])
->andFilterWhere(['like', 'slc_board', $this->slc_board])
->andFilterWhere(['like', 'slc_symbol', $this->slc_symbol])
->andFilterWhere(['like', 'hsc_school', $this->hsc_school])
->andFilterWhere(['like', 'hsc_board', $this->hsc_board])
->andFilterWhere(['like', 'hsc_symbol', $this->hsc_symbol])
->andFilterWhere(['like', 'section', $this->section])
->andFilterWhere(['like', 'semester', $this->semester]);
导致andFilterWhere不管理where字符串表示法然后以这种方式构建等效结果
if (isset($this->$this->admission_date)){
$query->andWhere('year(admission_date) = year (' . $this->admission_date. ')');
}