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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)