Yii2 - 模型中的自定义顺序获得关系

时间:2016-08-26 08:41:45

标签: yii yii2

在模型中获取自定义顺序的正确方法是什么?是否有可能做到这一点?这是我目前的代码:

   public function getBoardPosts()
        {
            return $this->hasMany(BoardPosts::className(), ['topic_id' => 'id'])->orderBy('order ASC');
        }

1 个答案:

答案 0 :(得分:1)

是的。以下是guide

中的示例
class Customer extends ActiveRecord
{
    public function getBigOrders($threshold = 100)
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])
            ->where('subtotal > :threshold', [':threshold' => $threshold])
            ->orderBy('id');
    }
}

请注意,您可能需要引用该字段(或者更好地使用非SQL字词命名您的列):

return $this->hasMany(BoardPosts::className(), ['topic_id' => 'id'])->orderBy('`order` ASC');