Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

php - Yii2 multiple model search in gridview

I had 2 model 'User' and 'UserProfile', I have displayed the data on the list page. I want to do filter for fields 'name_company' ... of model 'UserProfile' but I have no experience. Help me, thank all. My Index:

<?php
    $gridColumns = [
        ['class' => 'kartikgridCheckboxColumn'],
        [
            'attribute' => 'username',
            'value' => function($model){
                return Helper::checkRoute('update') ? Html::a($model->username, ['update', 'id' => $model->id]) : $model->username;
            },
            'format'=>'raw',
            'contentOptions' => [ 'style' => 'width: 10%;' ],
        ],
       ...
        [
            'label' => 'Name_company',
            'value' => function($model){
                $query = $model->userProfile->name_company;
                return $query;
            },
            'contentOptions' => [ 'style' => 'width: 15%;' ],
        ],
        ...
        ],
    ];

    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'options' => [ 'style' => 'table-layout:fixed;' ],
        'columns' => $gridColumns,
    ]);
    ?>

My index

My Controller:

public function actionIndex()
    {
        $searchModel = new UserSearch();
        $dataProvider = $searchModel->searchContractorUser(Yii::$app->request->queryParams);

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

My model Search, I had model search 'User':

public function searchContractorUser($params)
    {
        $query = User::find()->join('LEFT JOIN','rbac_auth_assignment','rbac_auth_assignment.user_id = id')
        ->where(['rbac_auth_assignment.item_name' => 'user-contractor']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

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

        $query->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'auth_key', $this->auth_key])
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'email', $this->email]);

        return $dataProvider;
    }
question from:https://stackoverflow.com/questions/65839053/yii2-multiple-model-search-in-gridview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...