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

时间:2014-08-21 11:58:16

标签: javascript php api backbone.js laravel

我有一个api,它返回一个“Groups”的JSON对象,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() {

    }

});

在我看来,我会做以下事情,

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

},

这会向组API端点发出GET请求。但是如果我在postman等地方运行端点,那么该集合只有2条记录而不是3条记录。

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

1 个答案:

答案 0 :(得分:1)

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