我有一个简单的关系1:N从单一模型中获得一些价格.
public function getPrices()
{
return $this->hasMany(Prices::className(), ['device_id' => 'id']);
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我需要按特定属性对价格对象进行排序 $value
我见过多个例子,Yii 1但没有Yii 2
感谢@vishu我试过这个:
public function getPrices()
{
return $this->hasMany(Prices::className(), ['device_id' => 'id'])
->viaTable(Prices::tableName(), ['device_id' => 'id'], function ($query) {
$query->orderBy(['device_price' => SORT_DESC]);
});
}
Run Code Online (Sandbox Code Playgroud)
但现在它返回一个空数组.
sca*_*dge 22
我认为您可以直接分配订单
public function getPrices()
{
return $this->hasMany(Prices::className(), ['device_id' => 'id'])->
orderBy(['device_price' => SORT_DESC]);
}
Run Code Online (Sandbox Code Playgroud)