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


API returning 3 records - backbone collection not matching

我有一个api,返回JSON对象"Groups", PHP为这是如下所示,

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);
}

返回以下JSON对象

[
{
    "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": []
}

)

我有一个使用这些数据的组集合(通过API填充),集合文件看起来像这样,

  var GroupCollection = Backbone.Collection.extend({
    url: '/groups/get',
    model: app.Group,
    initialize: function() {
    }
});

在我看来,我做了以下几点:

initial: 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();
},

将GET请求发送到组的API端点。但是集合只有2条记录,而不是3条,如果我以类似postman的方式运行端点,我将返回3条记录。

为什么我得到不同的结果?

您有两个具有相同id值的模型。集合中的每个模型必须有唯一的id