2015-09-10 17:06:05 4292次浏览 2条回答 1 悬赏 100 金钱

Yii 2.0.6

{
  "items": [
    {
      "id": 9,
      "uid": 1064,
      "comment": "artengo是迪卡侬的球拍类子品牌"
    },   
    {
      "id": 412,
      "uid": 1152,
      "comment": "Qqqqqqqqqq"
    }
  ],
  "_links": {
    "self": {
      "href": "http://api.restful.com/v1/reveal/list?document_id=7&fields=id%2Cuid%2Ccomment&page=1"
    },
    "next": {
      "href": "http://api.restful.com/v1/reveal/list?document_id=7&fields=id%2Cuid%2Ccomment&page=2"
    },
    "last": {
      "href": "http://api.restful.com/v1/reveal/list?document_id=7&fields=id%2Cuid%2Ccomment&page=4"
    }
  },
  "_meta": {
    "totalCount": 64,
    "pageCount": 4,
    "currentPage": 1,
    "perPage": 20
  }
}

1、怎么向_meta , _link中增加自定义的键值对
2、怎么增加类似的_meta,_link这样的自定义集合
3、expand不起作用呢?是这个字段储存的内容本身应该是对象吗?

  • 回答于 2015-09-10 17:19 举报

    Yii2的restful api函数(yii\rest\ActiveControlleraction),只要返回数组即可,所以12array_merge搞定。 问题3,需要在url参数中加expand键,其值对应于模型中的extraFields()函数返回数组中的值。通常expand用于取得关联表的结果集。

    觉得很赞
  • 回答于 2016-01-10 22:48 举报

    详细回答问题3:
    如果你在model中不override方法 extraFields()获取object,你在URL中添加expand字符是无效的。
    因为你想想index获得了一张表内容的内容,他怎么知道你的外键的对象。
    所以我们override此方法并返回相应的对象。示例如下,url?expand=room

    public function extraFields()
        {
            return ['room' => function(){
                return $this->room;
            }];
        }
    
您需要登录后才可以回答。登录 | 立即注册
trylife
总监

trylife Beijing

注册时间:2015-03-26
最后登录:2020-12-07
在线时长:51小时55分
  • 粉丝39
  • 金钱1626
  • 威望40
  • 积分2536

热门问题