如何使用php在firestore中创建子集合?

时间:2018-02-26 17:23:13

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

我希望使用此版本的PHP

创建this firestore structure
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

这是我的代码:

$path = "/xxx/firebase_auth.json";
$config = array(
    "projectId" => "test",
    "keyFile" => json_decode(file_get_contents($path), true)
);
$firestore = new FirestoreClient($config);
$collection = $firestore->collection('clients');
$trips = $collection->add([
    'organization_id' => '3'
]);

// this line throws an error when /employees is appended to path
$document = $firestore->document('clients/'.$trips->id().'/employees');

// this block works when '/employees' is removed from document path
// but it places the data in the document, not the '/employees' subcollection
$firestore->runTransaction(function (FirestoreTransaction $transaction) use ($document) {
    $transaction->create($document, [
        ['path' => 'roles', 'value' => array(
            "IT Manager" => "John",
            "Sales Manger" => "Jane",
            "Customer Service Manager" => "Jack",
            "Accounting Manager" => "Jill",
            "Managers" => 4
            )
        ],
        ['path' => 'office_location', 'value' => array(
            "latitude" => 40.7406375,
            "longitude" => -74.0107935
            )
        ],
        ['path' => 'country', 'value' => 'USA'],
    ]);
});

我运行时遇到此错误:

Exception 'InvalidArgumentException' with message 'Given path is not a valid document path.'
in /vendor/google/cloud/src/Firestore/FirestoreClient.php:248

我在这里阅读了Google Firestore子集:https://firebase.google.com/docs/firestore/data-model,并从该页面开始:

  

要了解分层数据结构在Cloud Firestore中的工作方式,     考虑一个带有消息和聊天室的示例聊天应用程序。     您可以创建一个名为rooms的集合来存储不同的聊天室。     现在您有聊天室,决定如何存储您的消息。你可能不会     想将它们存储在聊天室的文档中。 Cloud Firestore中的文档     应该是轻量级的,聊天室可能包含大量的     消息。但是,您可以在聊天中创建其他集合     会议室的文件,子收藏

但是,我使用的是Google Firestore的PHP SDK,并且未提及子集:http://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.53.0/firestore/transaction

如果可能,我只需要一个关于如何使用Firestore的PHP SDK创建子集合的示例。

谢谢!

0 个答案:

没有答案