在Firestore中创建文档时无法指定文档ID

时间:2018-03-15 21:14:52

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

我正在尝试在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

1 个答案:

答案 0 :(得分:0)

您可能不需要为一次写入编写批处理:

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

另外,查看文档。我们添加了PHP样本:

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