yii2 ActiveRecord Find OrderBy with calculation

自作多情 提交于 2019-12-10 01:21:43

问题


Trying to fetch a description from my database. The query returns the result but I would like to order the result to only show the one with the highest vote.

The vote should be calculated by the upvoted column subtracted by the downvoted column

$description = UnitDescription::find()
   ->where(['id_unit' => $model->id])
   ->orderBy([
      'upvoted - downvoted' => SORT_DESC //Need this line to be fixed
   ])
   ->one();

I was hoping someone might have a way to write this part of the query - Thanks


回答1:


You should simply try :

$description = UnitDescription::find()
    ->where(['id_unit' => $model->id])
    ->orderBy(['(upvoted - downvoted)' => SORT_DESC])
    ->one();


来源:https://stackoverflow.com/questions/34588843/yii2-activerecord-find-orderby-with-calculation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!