ActiveRecord where and order on via-table

前端 未结 8 740
南旧
南旧 2020-12-16 12:22

I have three database table:

product (id, name)

product_has_adv (product,advantage,sort,important)

advantage (id, text)

In ProductModel I def

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 13:06

    For who comes here after a while and don't like above solutions, I got it working by joining back to the via table after the filter via table.

    Example for above code:

    public function getAdvantages()
    {
        return $this->hasMany(AdvantageModel::className(), ['id' => 'advantage'])
            ->viaTable('product_has_advantage', ['product' => 'id'])
            ->innerJoin('product_has_advantage','XXX')
            ->orderBy('product_has_advantage.YYY'=> SORT_ASC);
    }
    

    Take care about changing XXX with the right join path and YYY with the right sort column.

提交回复
热议问题