dqndc26628 2018-05-06 21:58
浏览 70
已采纳

无法使用PHP通过Cloud Firestore从嵌套集合中获取文档

Following Google's official documentation at https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/firestore/src/get_all_docs.php, I am unable to retrieve documents that are part of a nested collection, i.e.:

$db = new FirestoreClient([
    'projectId' => $projectId,
]);
//$citiesRef = $db->collection('cities'); //working original example
$citiesRef = $db->collection('countries')->document('USA')->collection('cities'); //this throws an HTTP Error 500 
$documents = $citiesRef->documents();
foreach ($documents as $document) {
    if ($document->exists()) {
        printf('Document data for document %s:' . PHP_EOL, $document->id());
        print_r($document->fields());
        printf(PHP_EOL);
    } else {
        printf('Document %s does not exist!' . PHP_EOL, $snapshot->id());
    }
}

The above code throws an HTTP Error 500. Here is my example database:

Cloud Firestore Database Used in Example

The error log (CentOS 6) shows:

[06-May-2018 22:24:26 UTC] PHP Warning:  Module 'protobuf' already loaded in Unknown on line 0
[06-May-2018 22:24:26 UTC] PHP Warning:  Module 'protobuf' already loaded in Unknown on line 0
[06-May-2018 22:24:27 UTC] PHP Fatal error:  Call to undefined method Google\Protobuf\Timestamp::getFields() in /home/user/public_html/my/vendor/google/gax/src/ApiCore/Serializer.php on line 222
[06-May-2018 22:25:02 UTC] PHP Warning:  Module 'protobuf' already loaded in Unknown on line 0
[06-May-2018 22:25:02 UTC] PHP Warning:  Module 'protobuf' already loaded in Unknown on line 0

I'm aware that Firestore is still in beta. Is this one of the known issues/limitations?

  • 写回答

1条回答 默认 最新

  • doubaran2438 2018-05-08 06:40
    关注

    As a workaround, I am able to get the data from the nested document like so:

    $db = new FirestoreClient([
        'projectId' => $projectId,
    ]);
    //$citiesRef = $db->collection('cities'); //working original example
    $citiesRef = $db->collection('countries')->document('USA')->collection('cities');
    $documents = $citiesRef->select(['capital','country','name','population','state'])->documents();
    foreach ($documents as $document) {
        if ($document->exists()) {
            printf('Document data for document %s:' . PHP_EOL, $document->id());
            //print_r($document->fields()); //this no longer works
            print_r($document->data());
            printf(PHP_EOL);
        } else {
            printf('Document %s does not exist!' . PHP_EOL, $snapshot->id());
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题