[英]Firestore PHP - get one field from document
So I have a firestore that looks like this所以我有一个看起来像这样的firestore
and I want to be able to get just one value from a document, like getting the "uid" from the highlighted document, my php code looks like this:我希望能够从文档中只获取一个值,例如从突出显示的文档中获取“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;
}
}
?>
and the code calling it looks like this:调用它的代码如下所示:
<?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'));
I want to be able to retrieve just one field from a document in case I need for certain if statements or other conditional statements and for checking purposes我希望能够从文档中仅检索一个字段,以防我需要某些 if 语句或其他条件语句并用于检查目的
It seems you can only do this with NodeJS package for Cloud Firestore.看来您只能使用适用于 Cloud Firestore 的 NodeJS package 来执行此操作。 In this answer there's a link to the doc for firestore select which does this.
在这个答案中有一个指向firestore select 的文档的链接。
Just as any of the other Client Libraries, the PHP google/cloud-firestore
has the select method , which allows you to select the fields that you want to be retrieved.就像任何其他客户端库一样,PHP
google/cloud-firestore
具有select 方法,它允许您 select 检索您想要的字段。
I recommend checking the simple_queries.php
example , if you need further examples on how to set up these queries.如果您需要有关如何设置这些查询的更多示例,我建议您查看
simple_queries.php
示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.