如何在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',
];
}
}
答案 0 :(得分:1)
尝试使用http query发送订单
在你的情况下,它将是:
api/web/v1/news?sort=-news_id
答案 1 :(得分:0)
试试这个:
<?php $data = News::find()->orderBy('news_id DESC')->all() ?>