【问题标题】:Yii2: How to add two fields in orderby() of Find()Yii2:如何在 Find() 的 orderby() 中添加两个字段
【发布时间】:2016-06-15 08:53:44
【问题描述】:

如何在 find() 方法中添加多个字段进行排序?

我试过如下

$model::find()->orderBy([['id_date' => SORT_DESC],['item_no'=>SORT_ASC]);

但它会引发查询错误。 yii2产生的orderby查询为:ORDER BY 0, 1

【问题讨论】:

  • 这个question 可能会对你有所帮助。
  • @Jurik 我正在使用 find() 函数。不是 CActiveDataProvider。还有,我是yii2。

标签: php yii2


【解决方案1】:

根据documentation:

$model::find()->orderBy([
  'id_date' => SORT_DESC,
  'item_no'=>SORT_ASC
]);

【讨论】:

    【解决方案2】:

    您在以下代码中有语法错误:

    $model::find()->orderBy([['id_date' => SORT_DESC], ['item_no' => SORT_ASC]);
    

    正确的做法是:

    $model::find()->orderBy(['id_date' => SORT_DESC, 'item_no' => SORT_ASC]);
    

    【讨论】:

      【解决方案3】:
      class NewsController extends Controller
      {
      
          public function actionIndex ()
          {   $news = \common\models\News::find()->orderBy(['date' => SORT_DESC])->all();
              return $this->render("index",[
                  'news' => $news
              ]);
          }
      }
      

      【讨论】:

      • 请在您的代码中添加一些解释,以便其他人可以从中学习
      • 这没有回答问题(OP 询问了orderBy() 中的 2 个字段)。
      猜你喜欢
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多