douchuanghan1344 2014-08-21 11:58
浏览 41
已采纳

API返回3条记录 - 主干集合不匹配

I have an api that returns a JSON object of "Groups", the PHP for this is as follows,

public function index()
{
    $teams = new Team;
    $clients = new Client;
    $organisations = new Organisation;

    //$org = Organisation::find(1);
    //return json_encode($org->clients());

    // Get the organisations
    $org = Organisation::all()->toArray();

    $organisations = array();

    foreach( $org as $k => $v ) {
        $organisations[$k]['id'] = $v['id'];
        $organisations[$k]['name'] = $v['name'];
        $organisations[$k]['information'] = $v['information'];
        $organisations[$k]['notifications'] = $v['notifications'];
        $organisations[$k]['add_all'] = $v['add_all'];
        $organisations[$k]['created_at'] = $v['created_at'];
        $organisations[$k]['updated_at'] = $v['updated_at'];
        $organisations[$k]['type'] = $v['type'];
        $organisations[$k]['clients'] = Organisation::find($v['id'])->clients;
        $organisations[$k]['projects'] = Organisation::find($v['id'])->projects;
        $organisations[$k]['members'] = Organisation::find($v['id'])->users;
        $organisations[$k]['teams'] = Organisation::find($v['id'])->teams;
    }

    // Get the clients
    $cli = Client::all()->toArray();

    $clients = array();

    foreach( $cli as $k => $v) {
        $clients[$k]['id'] = $v['id'];
        $clients[$k]['name'] = $v['name'];
        $clients[$k]['information'] = $v['information'];
        $clients[$k]['add_all'] = $v['add_all'];
        $clients[$k]['created_at'] = $v['created_at'];
        $clients[$k]['updated_at'] = $v['updated_at'];
        $clients[$k]['type'] = $v['type']; 
        $clients[$k]['members'] = Client::find($v['id'])->users;
    }


    // Get the teams
    $team = Team::all()->toArray();

    $teams = array();

    foreach( $team as $k => $v ) {
        $teams[$k]['id'] = $v['id'];
        $teams[$k]['name'] = $v['name'];
        $teams[$k]['information'] = $v['information'];
        $teams[$k]['created_at'] = $v['created_at'];
        $teams[$k]['updated_at'] = $v['updated_at'];
        $teams[$k]['type'] = $v['type']; 
        $teams[$k]['members'] = Team::find($v['id'])->users;
    }

    $result = array_merge($organisations, $clients, $teams);

    return Response::json($result, 200);

}

This returns the following JSON object,

[
{
    "id": "1",
    "name": "Organisation",
    "information": "This is some information about the organisation. ",
    "notifications": "0",
    "add_all": "0",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "organisation",
    "clients": [],
    "projects": [],
    "members": [],
    "teams": []
},
{
    "id": "1",
    "name": "Client",
    "information": "",
    "add_all": "0",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "client",
    "members": []
},
{
    "id": "2",
    "name": "Developers",
    "information": "",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "type": "team",
    "members": []
}

]

I have a groups collection that uses this data (populated via the API), the collection file looks like this,

  var GroupCollection = Backbone.Collection.extend({

    url: '/groups/get',
    model: app.Group,

    initialize: function() {

    }

});

In my view I do the following,

nitialize: function() {

    var that = this;

    this.filteredCollection = new app.userFilteredCollection;

    this.$el.find("h4").text("Edit " + this.model.get('name'));

    this.$el.attr('id', 'editProject');

    this.$el.find(".modal-body").html( this.template(this.model.toJSON() ) );

    this.groupsCollection = new GroupCollection;
    this.groupsCollection.fetch();

    this.render();

},

This fires the GET request to the groups API endpoint. BUT the collection only has 2 records not 3 that I get returned if I run the endpoint in something like postman.

Why would I get differing results?

  • 写回答

1条回答 默认 最新

  • dongpu1908 2014-08-21 12:07
    关注

    You have 2 models with identical id values. Each model in a collection must have a unique id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制