数据小部件过滤数据Yii2

时间:2016-07-03 15:12:41

标签: php yii2 datagridview

我将数据小部件与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',过滤工作。请帮助。

1 个答案:

答案 0 :(得分:0)

也许您忘了在$ model-&gt; rules()方法中将属性包含在“安全”数组中。

[['post_id'], 'safe'],

尝试

var_dump($this->post_id);

在搜索模型中“$ this-&gt; load($ params)”之后,看看有什么。