A
A
akula222016-10-07 16:24:22
Yii
akula22, 2016-10-07 16:24:22

How to correctly display category names in gridview?

In the grid, you need to display the name of the category, we have a category_id
, you can do this:

[
              'attribute' => 'category_id', 
                'content'=>function($data) {
                    return $data->getCategoryName();
                },
]

and in the model by connection to get the name
public function getParent()
    {
        return $this->hasOne(\app\modules\team\models\TeamCategory::className(), ['id' => 'category_id']);
    }
public function getCategoryName()
    {
        $parent = $this->parent;
        return $parent->title ;
    }

and we get 20 extra queries in the database.
And in this example, I have 20 entries with one category, and it makes no sense to make 20 requests, how can I make that the request was one, somehow first get a list of category IDs and only make one request for them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-10-07
@akula22

extra requests are caused by the fact that you are using lazy loading, and to output data to the grid it is better to use eager loading , in particular joinWith

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question