Yii2 - 在detailview中搜索确切的值并返回结果

时间:2016-08-06 20:42:50

标签: yii2

我需要在页面上按协议编号进行搜索,当输入的协议编号完全相同时,只显示一个结果。如果可能的话显示结果,它是一个DetailView

我的模型OccurrenceSearch

    public function searchprotocol($params)
    {
        $query = Occurrence::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'protocol' => $this->protocol,
        ]);

        return $dataProvider;
    }    
}

我的观点search

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

?>

<div class="occurrence-search">

    <?php $form = ActiveForm::begin([
        'action' => ['search'],
        'method' => 'get',
    ]); ?>

    <?= $form->field($model, 'protocol') ?>

    <div class="form-group">
        <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

我的控制器actionSearch()

public function actionSearch()
{
    $searchModel = new OccurrenceSearch();
    $dataProvider = $searchModel->searchprotocol(Yii::$app->request->queryParams);

    return $this->render('search', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

来宾用户只能看到其协议编号的记录

2 个答案:

答案 0 :(得分:1)

一个detailView使用模型(而不是dataProvider)来显示数据实例.. 然后在你的控制器中你应该获得模型

public function actionSearch()
{
    $searchModel = new OccurrenceSearch();
    $dataProvider = $searchModel->searchprotocol(Yii::$app->request->queryParams);
    $model = $dataProvider->query->one();

    return $this->render('search', [
        'searchModel' => $searchModel,
        //'dataProvider' => $dataProvider,
        'model' => $model,
    ]);
}

但是在你看来,建议的代码不显示detailView小部件..你应该添加

答案 1 :(得分:0)

我决定使用GRIDVIEW,因此更改了MymodelSearch

if(isset($ _ GET [&#39; AuthorSearch&#39;])&amp;&amp;!($ this-&gt; load($ params)&amp;&amp; $ this-&gt; validate())) {     return $ dataProvider; }