gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:00:53 25 4
gpt4 key购买 nike

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

}

});

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

初始化:函数() {

    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 条记录。

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

最佳答案

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

关于javascript - API 返回 3 条记录 - 主干集合不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25425786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com