【问题标题】:Cannot Specify Document Id When Creating Document in Firestore在 Firestore 中创建文档时无法指定文档 ID
【发布时间】:2018-03-15 21:14:51
【问题描述】:

我正在尝试在 Google Firebase 中创建一个节点,并使用其唯一 ID 在 Google Firestore 中创建一个同名文档。

我正在使用 Google 的 PHP Firestore 客户端:https://github.com/GoogleCloudPlatform/google-cloud-php-firestore

我已经阅读了他们的文档:http://googlecloudplatform.github.io/google-cloud-php/#/docs/cloud-firestore/v0.5.1/firestore/writebatch

这是我的代码:

<?php

use \Google\Cloud\Firestore\FirestoreClient;
use \Google\Cloud\Core\Timestamp;
use \Google\Cloud\Firestore\Transaction as FirestoreTransaction;
use \grptx\Firebase as FirebaseClient;

class FirestoreTest
{
    public function create()
    {
        $client = new FirebaseClient();
        $database = $client->getDatabase();

        $org = array(
            "acl" => array(),
            "people" => array()
        );

        $ref = $database->getReference("/clients/")->push($org);
        $key = $ref->getKey();

        $config = array(
            "projectId" => "xxx",
            "keyFile" => json_decode(file_get_contents("/xxx/firebase_auth.json"), true)
        );

        $firestore = new FirestoreClient($config);
        $batch = $firestore->batch();
        $batch->create("/clients/".$key, [
            'organization_ID' => $key
        ]);

        print_r($key); // this key gets generated, no errors occur.
    }
}

这是我正在使用的 PHP 版本:

php -v
PHP 7.1.10-1+ubuntu14.04.1+deb.sury.org+1 (cli) (built: Sep 29 2017 17:33:22) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.10-1+ubuntu14.04.1+deb.sury.org+1, Copyright (c) 
1999-2017, by Zend Technologies

【问题讨论】:

    标签: php firebase google-cloud-firestore php-7.1


    【解决方案1】:

    单次写入可能不需要写入批处理:

    $docRef = $firestore->collection('clients')->document($key);
    $docRef->set(['organization_ID' => $key]);
    

    另外,请查看文档。我们添加了 PHP 示例:

    https://cloud.google.com/firestore/docs/manage-data/add-data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多