Yii2搜索模型相关模型

时间:2017-06-01 09:43:36

标签: search yii2

正如你所看到的,我是gridview(来自名为Nomi的模型)。并通过 id_nomi 我展示了nomi.rus(这意味着来自nomi模型的rus专栏):

enter image description here

这里的问题是我正试图通过umumiy gridview从nomi.rus模型进行搜索。我正在尝试通过ajax获取值(使用 $model = new UmumiyModel(); $searchModel = new UmumiyModelSearch(); if (Yii::$app->request->isAjax){ $data = Yii::$app->request->post(); $searchModel->nomi->rus = $data['dori_nomi']; // search input value $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->renderPartial('sotish', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'model' => $model, ]); } )。这就是我试过的:

<?= $sffForm->field($sffModel, 'url_keywords', ['enableLabel' => false])->textInput(['placeholder' => 'URL / keywords']) ?>

我做错了什么???

1 个答案:

答案 0 :(得分:1)

您可以使用NomiSearch模型中的公共成员来存储来自&#34; Id Nomi&#34; gridview的输入字段。

所以,在 NomiSearch 模型中:

class NomiSearch extends Nomi
{
    public $nomiText;

    public function rules()
    {
        return [
            // ...
            [['nomiText'], 'safe'],
        ];
    }

    public function search($params)
    {
        $query = Nomi::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([
        ]);

        if($this->nomiText!=null)
        {
            $query->andWhere(['IN', 'id_nomi', (new \yii\db\Query())->select('id')->from('nomi')->where(['like', 'nomi', $this->nomiText])]);
        }   

        return $dataProvider;
    }

}

最后,在索引视图中:

GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'label' => 'Id Nomi',
                'attribute' => 'nomiText',
                'value' => function($data) {
                    return $data->nomi->rus;
                },

            ],