【问题标题】:PHP Google Firestore | How to query by reference field?PHP 谷歌 Firestore |如何按引用字段查询?
【发布时间】:2021-04-08 09:34:31
【问题描述】:

$ref->where('reference_field', '=', 'Path/To/Referenced/Document'); 不起作用。

<?php

namespace Google\Cloud\Samples\Firestore;

use Google\Cloud\Firestore\FirestoreClient;

function query_by_reference_field($projectId)
{
    // Create the Cloud Firestore client
    $db = new FirestoreClient([
        'projectId' => $projectId,
    ]);

    $ref = $db->collection('CollectionName');
    $query = $ref->where('reference_field', '=', 'Path/To/Referenced/Document');
    $snapshot = $query->documents();
    foreach ($snapshot as $document) {
        printf('Document %s returned.' . PHP_EOL, $document->id());
    }
}

那么,如果引用字段包含给定的文档路径/文档 ID,我如何获得结果?

【问题讨论】:

  • @RiggsFolly 是的,我正在使用该框架。我刚刚编辑了我的问题,添加了指向完整代码查询示例的链接。
  • 你发现'reference field'没有下划线
  • 我刚刚做了,但这不是问题。
  • @RiggsFolly 我想通了。我刚刚回答了我自己的问题。感谢您的宝贵时间,如果我来的时候听起来有点粗鲁,我们深表歉意。

标签: php google-cloud-firestore


【解决方案1】:

我刚刚通过查看这个javascript问题here弄明白了。

答案是:你必须传递一个文档引用对象而不是路径字符串。

<?php

namespace Google\Cloud\Samples\Firestore;

use Google\Cloud\Firestore\FirestoreClient;

function query_by_reference_field($projectId)
{
    // Create the Cloud Firestore client
    $db = new FirestoreClient([
        'projectId' => $projectId,
    ]);
    
    $referenced_document = $db->document('Path/To/Referenced/Document');

    $ref = $db->collection('CollectionName');
    $query = $ref->where('reference_field', '=', $referenced_document);
    $snapshot = $query->documents();
    foreach ($snapshot as $document) {
        printf('Document %s returned.' . PHP_EOL, $document->id());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 2023-03-27
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多