发送Yii2 REST GET请求时如何按相关表字段排序
我想扩展 基本上我有发送Yii2 REST GET请求时如何按相关表字段排序,rest,sorting,activerecord,yii2,Rest,Sorting,Activerecord,Yii2,我想扩展 基本上我有用户端点。但我也从相关的配置文件表返回数据。我没有扩展配置文件,我总是想返回它。所以我有这样的方法: public function fields() { $fields = parent::fields(); $fields[] = 'profile'; return $fields; } 当我按profile.created_字段和user.status获取请求和请求排序时,它不会按profile.created_字段排序 获取v1/users?
用户
端点。但我也从相关的配置文件表返回数据。我没有扩展配置文件,我总是想返回它。所以我有这样的方法:
public function fields()
{
$fields = parent::fields();
$fields[] = 'profile';
return $fields;
}
当我按profile.created_字段和user.status获取请求和请求排序时,它不会按profile.created_字段排序
获取v1/users?sort=-profile.created_at,status
这能以某种方式实现吗
这是我当前的代码:
/** @var $query ActiveQuery */
$query = User::find();
// get data from profile table
$query->innerJoinWith('profile');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
'pagination' => [
'pageSize' => 10,
],
]);
return $dataProvider;
您已重写ActiveDataProvider的“sort”参数。要保持排序对象的默认行为并更改defaultOrder属性,请创建一个实例,例如:
$sort = new \yii\data\Sort([
'attributes' => [
'profile.created_at',
],
'defaultOrder' => ['id' => SORT_DESC],
]);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => $sort,
'pagination' => [
'pageSize' => 10,
],
]);
您必须查看Yi2范围,它可能会对您有所帮助。这与我上面发布的代码没有任何不同。我仍然无法按配置文件进行排序。如何消除列的歧义?我已经在users表中创建了_at,在profile表中创建了_at,并希望按profile排序。created_at。这不起作用:$sort=new\yii\data\sort(['attributes'=>['profile.created\u at',],'defaultOrder'=>['profile.created\u at'=>sort\u DESC],])代码>它表示未定义的索引