直接上代码
感谢 老胡哥,分享代码,记录一下,方便学习,且分享给大家
https://github.com/hubeiwei/hello-yii2/blob/2.0/models/search/SettingSearch.php
https://github.com/hubeiwei/hello-yii2/blob/2.0/modules/backend/views/setting/index.php
user表主键id;setting表 外键updated_by
SettingSearch.php
'操作人',
]);
}
/**
* @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 = self::find()
->from(['setting' => self::tableName()])
->select([
'setting.*',
'user.username',
])
->leftJoin(['user' => User::tableName()], 'user.id = setting.updated_by');
// 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([
'setting.id' => $this->id,
'setting.updated_by' => $this->updated_by,
'user.username' => $this->getAttribute('username'),
]);
$query->andFilterWhere(['like', 'key', $this->key])
->andFilterWhere(['like', 'value', $this->value])
->andFilterWhere(['like', 'setting.status', $this->status])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'tag', $this->tag]);
$query->timeRangeFilter('setting.created_at', $this->created_at, false)
->timeRangeFilter('setting.updated_at', $this->updated_at, false);
return $dataProvider;
}
}
title = '网站配置';
$this->params['breadcrumbs'][] = ['label' => $this->title, 'url' => ['index']];
$gridColumns = [
['class' => SerialColumn::className()],
[
'attribute' => 'id',
'headerOptions' => ['width' => 80],
],
'key',
'value',
'description',
'tag',
[
'attribute' => 'status',
'value' => function ($model) {
return Setting::$status_map[$model->status];
},
'filter' => RenderHelper::dropDownFilter('SettingSearch[status]', $searchModel->status, Setting::$status_map),
'headerOptions' => ['width' => 100],
],
'username',
[
'attribute' => 'created_at',
'format' => 'dateTime',
'filterType' => DateRangePicker::className(),
'filterWidgetOptions' => [
'dateOnly' => false,
],
'headerOptions' => ['width' => 160],
],
[
'attribute' => 'updated_at',
'format' => 'dateTime',
'filterType' => DateRangePicker::className(),
'filterWidgetOptions' => [
'dateOnly' => false,
],
'headerOptions' => ['width' => 160],
],
['class' => ActionColumn::className()],
];
?>
= Html::encode($this->title) ?>
= Html::a('添加配置', ['create'], ['class' => 'btn btn-info']) ?>
= RenderHelper::gridView($dataProvider, $gridColumns, $searchModel) ?>