来自yii2明确指南:
public function fields()
{
return ['id', 'email'];
}
public function extraFields()
{
return ['profile'];
}带有http://localhost/users?fields=id,email&expand=profile的请求可能返回以下JSON数据:
[
{
"id": 100,
"email": "100@example.com",
"profile": {
"id": 100,
"age": 30,
}
},
...
]在这个示例中,我如何调优extraFields (或者其他一些东西),使其只获得响应配置文件部分中的一个字段(例如,年龄)?
发布于 2018-02-05 14:05:10
此特性将在YI2.0.14 https://github.com/yiisoft/yii2/pull/14219中添加。
示例API请求的工作方式如下:users?fields=id,email&expand=profile.age
编辑:现在您可以使用如下内容:
public function extraFields()
{
return [
'profileAge' => function($item){
return $item->profile->age
}
];
}请求:http://localhost/users?fields=id,email&expand=profileAge
发布于 2016-06-15 10:48:14
想到的第一件事
public function extraFields(){
return [
'profile' => function($item){
return [
'age' => $item->profile->age
];
}
];
}https://stackoverflow.com/questions/37831902
复制相似问题