根据firebase文档,https://www.firebase.com/docs/rest-api.html,它声明:
PATCH - Updating Data
You can update specific children at a location without overwriting existing data
with a PATCH request. Named children in the data being written with PATCH will be
written, but omitted children will not be deleted. This is equivalent to the
update( ) function.
curl -X PATCH -d '{"last":"Jones"}' \
https://SampleChat.firebaseIO-demo.com/users/jack/name/.json
A successful request will be indicated by a 200 OK HTTP status code.
The response will contain the data written:
{"last":"Jones"}
现在我对此的理解是,如果我希望只更新资源的一部分,那么我可以使用补丁请求。
我简化的firebase数据库如下所示:
"exchange-rates" : {
"eur" : {
"fx" : 1.2,
"currency_symbol" : "€",
"updated_at" : "2014-06-13T22:49:23+0100",
},
"usd" : {
"fx" : 1.6,
"currency_symbol" : "$",
"updated_at" : "2014-06-13T22:49:23+0100",
},
"gbp" : {
"fx" : 1,
"currency_symbol" : "£",
"updated_at" : "2014-06-16T15:43:15+0100",
}
}
但是,如果我在修补程序请求的有效负载中省略了currency_symbol
和updated_at
,则Firebase将从数据库中删除这些属性。
$auth = 'SUPER_SECRET_CODE';
$guzzleClient = new GuzzleHttp\Client();
$url = https://DATABASE.firebaseio.com/.json;
$data['exchange-rates']['gbp']['fx'] = (float) 1;
$data['exchange-rates']['usd']['fx'] = (float) 1.66;
$data['exchange-rates']['eur']['fx'] = (float) 1.22;
$payload =
[
'query' => [ 'auth' => $auth ],
'json' => $data
];
$response = $guzzleClient->patch($url, $payload);
因此,补丁请求没有正常工作,或者我误解了Firebase应该如何处理这个补丁请求-或者我遗漏了一些东西。有什么想法吗?
此外,如果我希望向汇率对象添加一个对象,我应该能够这样做。
$data['exchange-rates']['chf']['fx'] = 2.13;
$payload =
[
'query' => [ 'auth' => $auth ],
'json' => $data
];
$response = $guzzleClient->patch($url, $payload);
然而,所有这一切只是覆盖所有现有的汇率,现在我在数据库中只有一个汇率。
发布于 2014-06-25 12:41:15
更新/修补操作不是递归的。它们只观察更新的直接子项的键。示例:
// assume data: { foo: 'bar', baz: {uno: 1, dos: 2}, zeta: {hello: 'world'} };
curl -X PATCH -d '{"tres": 3}' $URL
// baz is now: {uno: 1, dos: 2, tres: 3}
curl -X PATCH -d '{"foo": "barr", baz: {"tres": 3}}' $URL
// baz is now {tres: 3}
所以更新只有一个级别的深度。如果您提供的其中一个子记录是一个对象,它将替换该路径中的子记录,而不是尝试合并这两个记录。
https://stackoverflow.com/questions/24295620
复制相似问题