3

根据 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"}

现在我对此的理解是,如果我只想更新资源的一部分,那么我可以使用 PATCH 请求。

我简化的 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_symboland 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);

因此,PATCH 请求无法正常工作,或者我误解了 Firebase 应该如何处理此 PATCH 请求 - 或者我遗漏了一些东西。有什么想法吗?

另外,如果我想向 exchange-rates 对象添加一个对象,我应该可以这样做。

$data['exchange-rates']['chf']['fx'] = 2.13;

    $payload =
        [
            'query' => [ 'auth' => $auth ],
            'json' => $data
        ];

    $response = $guzzleClient->patch($url, $payload);

然而,这一切只是覆盖了所有现有的汇率,现在我在数据库中只有 1 个汇率。

4

1 回答 1

2

更新/补丁操作不是递归的。他们只观察更新的直接子项的键。例子:

// 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}

所以更新只有一级深。如果您提供的子记录之一是对象,它将替换该路径上的子记录,而不是尝试合并两者。

于 2014-06-25T04:41:15.107 回答