0

如何在 YII2 ActiveController 中按降序返回数据?请帮我。从 activecontroller 返回的 JSON 响应必须按 news_id 降序排列。

 <?php
    namespace app\api\modules\v1\controllers;
    use yii\web\Response;
    use yii\rest\ActiveController;

    class NewsController extends ActiveController {
      // We are using the regular web app modules:
      public $modelClass = 'app\models\News';
    }

这是新闻模型

 <?php

    namespace app\models;

    use Yii;
    class News extends \yii\db\ActiveRecord
    {

        public static function tableName()
        {
            return 'news';
        }


        public function rules()
        {
            return [
                [['news_title', 'news_description', 'news_link'], 'required'],
                [['news_description', 'news_link'], 'string'],
                [['news_time'], 'safe'],
                [['news_title'], 'string', 'max' => 255],
            ];
        }


        public function attributeLabels()
        {
            return [
                'news_id' => 'News ID',
                'news_title' => 'News Title',
                'news_description' => 'News Description',
                'news_link' => 'News Link',
                'news_time' => 'News Time',
            ];
        }
    }
4

2 回答 2

1

尝试使用 http查询发送订单 在您的情况下,它将是: api/web/v1/news?sort=-news_id

于 2017-08-30T11:32:18.897 回答
0

尝试这个:

<?php $data = News::find()->orderBy('news_id DESC')->all()  ?>
于 2017-09-26T12:11:21.113 回答