简体   繁体   中英

Yii2 Model ViaTable

For simplicity, lets say I have 3 tables: menu , page and a junction table menu_page .

So If I want to get all menus which are available for page "home", in the model I defined a relationship:

public function getAllMenus() {
    return $this->hasMany(Menu::className(), ['id' => 'menu_id'])->viaTable(PageMenu::tableName(), ['page_id' => 'id']);
}

But now we have added an attribute to menu table called show_all_pages , if this is set as 1, menu should be returned, if not we should check if menu is enabled to be used on home.

Is there a way to add this condition here?

You can make use of an andWhere clause to apply this filter on show_all_pages

public function getAllMenus() {
    return $this->hasMany(Menu::className(), ['id' => 'menu_id'])
                ->viaTable(PageMenu::tableName(), ['page_id' => 'id'])
                ->andWhere(['show_all_pages' => 1]);
}

The solution I found so far was doing to separate Active Queries and do a Union:

public function getSelectedMenus() {

        return $this->hasMany(Menu::className(), ['id' => 'menu_id'])->viaTable(PageMenu::tableName(), ['page_id' => 'id'])->onCondition(['menu.active' => Page::ACTIVE]);
    }

    public function getAllMenus() {
        return Menu::find()->where(['active' => Page::ACTIVE, 'show_all_pages' => 1]);
    }

    public function getMenus() {

        $selectedMenus = $this->getSelectedMenus();
        $allMenus = $this->getAllMenus();

        return $selectedMenus->union($allMenus);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM