S
S
Sergey Melnikov2016-01-04 15:21:48
Yii
Sergey Melnikov, 2016-01-04 15:21:48

How to correctly display a linked table in a GridView?

The content model has an association with a category.
public function getCategory(){
return $this->hasOne(DataCategory::className(), ['id_category' => 'category'])
->viaTable('data_subcategory', ['id_subcategory' => 'subcategory']) ;
}
Works.
But another problem arose, now when I call 'category.category_name' in the gridview,
it makes 2 queries for each record - one to the "category" table, the second to the "subcategory" table
SELECT * FROM `data_subcategory` WHERE `id_subcategory`=1
SELECT * FROM `data_category` WHERE `id_category`='1'
How to overcome this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander N++, 2016-01-04
@azazel_live

When you pass a Query to an ActiveDataProvider,
you can set the Query model to joinWith(['category']);
Then there will be 2-3~ requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question