我使用数据小部件与pjux和需要排序我的日期。我试着用这个http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html。但在过滤方面有困难。我SiteController
$PostView = Posts::findOne($id);
$model = new Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->user_id = Yii::$app->user->getId();
$model->post_id = $id;
if ($model->save()):
$model = new Comments();
Yii::$app->session->setFlash('success', "OK");
else: Yii::$app->session->setFlash('error', "NOT");
Yii::error('Some error');
return $this->refresh();
endif;
} else {
$searchModel = new CommentsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('view', [
'PostView' => $PostView,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
我的搜索控制器
$query->andFilterWhere([
'id' => $this->id,
'post_id' => $this->post_id,
'user_id' => $this->user_id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'comment', $this->comment]);
return $dataProvider;
在视图:<?php Pjax::begin(['id' => 'comments']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii'grid'SerialColumn'],
'post_id',
'comment:ntext',
],
]); ?>
<?php Pjax::end() ?>
同样,如果我改变'post_id' => $this->post_id,
对'post_id' => '1',
过滤工作。请帮助。
可能您忘记在$model->rules()方法中将属性包含到"safe"数组中。
[['post_id'], 'safe'],
尝试
var_dump($this->post_id);
在"$this->load($params)"之后查看你的搜索模型