【问题标题】:How do I create subcollections in firestore with php?如何使用 php 在 firestore 中创建子集合?
【发布时间】:2018-08-06 05:23:48
【问题描述】:

我希望用这个版本的 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 中分层数据结构的工作原理, 考虑一个带有消息和聊天室的示例聊天应用程序。 您可以创建一个名为房间的集合来存储不同的聊天室。 现在您有了聊天室,决定如何存储您的消息。你可能不会 想将它们存储在聊天室的文档中。 Cloud Firestore 中的文档 应该是轻量级的,一个聊天室可以包含大量的 消息。但是,您可以在聊天中创建其他集合 房间的文档,作为子集合。

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

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    如果您只是创建一个文档,您可能不需要事务:​​

    $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'],
        ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 2020-04-07
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      相关资源
      最近更新 更多