S
S
Sergey Melnikov2015-01-01 20:22:09
Yii
Sergey Melnikov, 2015-01-01 20:22:09

How to make a hierarchical relationship between models in yii2?

There is a category
model There is a subcategory model
There is a content catalog model
How to access the category model in the gridview, and the output of the records is based on the content model?
Relationships between models are as follows: category model has a one-to-many relationship to subcategory, subcategory to content.
******** addition
As a result, I figured out what needs to be done through viaTable
public function getCategory(){
return $this->hasOne(DataCategory::className(), ['id_category' => 'category'])
-> viaTable('data_subcategory', ['id_subcategory' => 'subcategory']);
}
But there was another problem, now when calling in the gridview 'category.category_name',
he 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'
Like this overcome?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Natarov, 2015-01-01
@HanDroid

Your example?

S
Sergey Melnikov, 2016-01-04
@azazel_live

Tell me how to overcome bulk requests in the viaTable mechanism

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question