[英]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 的文檔的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.