Answer the question
In order to leave comments, you need to log in
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
Tell me how to overcome bulk requests in the viaTable mechanism
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question