K
K
kanly2020-04-29 14:22:24
Yii
kanly, 2020-04-29 14:22:24

How to solve yii2 GridView search problems?

Good afternoon! I created an admin panel with a gridview widget via crud. And I changed the display in the index file

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ['class' => 'yii\grid\ActionColumn'],

            // 'id',
            'name',
            'price',
            //'category_id',
            [
                'attribute'=>'category_id',
                'format'=>'raw',
                'value'=>function($data){
                    $catr = Category::find()->where(['id'=>$data->category_id])->one();
                    return $catr->name;
                }
            ],

        ],
    ]); ?>


as you can see, I replaced category_id with category name, and how to change category_id to category name in the search, otherwise it is still looking for category_id, I need to search for category name

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Kupletsky, 2020-05-05
@zavoloklom

You need to change the SearchModel: add the category_name property there and change the database query so that on the one hand this property is correctly selected (add to select and do not forget about JOIN), and on the other hand, it is searched for (if set, then add the corresponding andFilterWhere ).
Then instead of category_id in GridView it will be possible to use category_name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question