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

How do I create subcollections in firestore with php?

我希望使用此版本的 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,并从该页面阅读:

To understand how hierarchical data structures work in Cloud Firestore, consider an example chat app with messages and chat rooms. You can create a collection called rooms to store different chat rooms. Now that you have chat rooms, decide how to store your messages. You might not want to store them in the chat room's document. Documents in Cloud Firestore should be lightweight, and a chat room could contain a large number of messages. However, you can create additional collections within your chat room's document, as subcollections.

但是,我使用的是 Google Firestore 的 PHP SDK,没有提到子集合:http://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.53.0/firestore/transaction

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

谢谢!

您不需要显式创建子集合。创建对子集合的引用,您就可以开始添加文档了。

如果您只是创建一个文档,您可能不需要交易:

$employeesCollection = $firestore->collection('clients/'.$trips->id().'/employees');

$newDocument = $employeesCollection->add([
        ['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'],
    ]);