繁体   English   中英

Firestore PHP - 从文档中获取一个字段

[英]Firestore PHP - get one field from document

所以我有一个看起来像这样的firestore

在此处输入图像描述

我希望能够从文档中只获取一个值,例如从突出显示的文档中获取“uid”,我的 php 代码如下所示:

<?php

    require 'vendor/autoload.php';
    //putenv('upheld-pursuit-274606-237ac40b2662.json');
    use Google\Cloud\Firestore\FirestoreClient;

    class Firestore
    {
        protected $db;
        protected $name;
        public function __construct(string $collection)
        {
            $this->db = new FirestoreClient([
                'keyFilePath' => 'upheld-pursuit-274606-237ac40b2662.json',
                'projectId' => 'upheld-pursuit-274606'
            ]);

            $this->name = $collection;
        }


        public function getWhere(string $field, string $operator, $value)
        {
            $arr = [];
            $query = $this->db->collection($this->name)->where($field, $operator, $value)->documents()->rows();
            if(!empty($query)){
                foreach ($query as $q){
                    $arr[] = $q->data();
                }
            }
            return $arr;
        }

        public function get(string $field)
        {
            $arr = [];
            $query = $this->db->collection($this->name)->document($field)();
            if(!empty($query)){
                /*foreach ($query as $q){
                    $arr[] = $q->data();
                }*/
                throw new Exception('Document does not exist!');
            }
            return $query;
        }

    }
?>

调用它的代码如下所示:

<?php

session_start();

require("connect.php");
require_once 'vendor/autoload.php';
require_once 'Firestore.php';

$fs = new Firestore('users');
$tfs = new Firestore('tutors');


    $username = mysqli_real_escape_string($conn, $_GET['name']);

    $password = mysqli_real_escape_string($conn, $_GET['pass']);

    $password = md5($password);

    $data = array();

    print_r($fs->get('1WINXTQdshhn4jLfhMWWaZNNdL32'));

我希望能够从文档中仅检索一个字段,以防我需要某些 if 语句或其他条件语句并用于检查目的

看来您只能使用适用于 Cloud Firestore 的 NodeJS package 来执行此操作。 这个答案中有一个指向firestore select 的文档的链接。

就像任何其他客户端库一样,PHP google/cloud-firestore具有select 方法,它允许您 select 检索您想要的字段。

如果您需要有关如何设置这些查询的更多示例,我建议您查看simple_queries.php 示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM