Retrieve data from junction table in Yii2

前端 未结 4 1213
离开以前
离开以前 2021-02-01 09:18

I\'m trying to get data from a join table in Yii2 without an additional query. I have 2 models (User, Group) associated via the junction table (user_group). In

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 10:18

    For that purpose I've created a simple extension, that allows to attach columns in junction table to child model in relation as properties. So after setting up this extension you will be able to access junction table attributes like

    foreach ($parentModel->relatedModels as $childModel)
    {
        $childModel->junction_table_column1;
        $childModel->junction_table_column2;
        ....
    }
    

    For more info please have look at Yii2 junction table attributes extension

    Thanks.

提交回复
热议问题