Cloud Datastore PHP 返回 0 个结果

Cloud Datastore PHP returning 0 results

所以我首先尝试查看我的 PHP 是否可以与 Datastore 通信并检索数据。 我在 "keypad_research".

类型下创建了两个实体

这是我的 PHP 的样子:

<?php
require __DIR__ . '/../../vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Datastore\Entity;
$projectId = "__my projectID__";
$datasetId = $projectId;
$datastore = new DatastoreClient(['projectId' => $projectId]);

function getlist($datastore){
    $query = $datastore->query()
        ->kind('keypad_research')
        ->start($cursor);

    $results = $datastore->runQuery($query);
    $entries = [];
    $count = 0;
    foreach ($results as $entity) {
        $count++;
    }

    echo $count; // this shows me '0' results even when I have 2 entities.
}
getlist($datastore);
?>

正如您在最后的 echo 语句中看到的那样,结果是 0 行数据。

我是否需要进行任何附加配置或编辑我的 PHP 才能从数据存储中读取?

我使用 PHP 从数据存储中读取数据的基本尝试失败了。

如有任何建议,我们将不胜感激。

我已经能够使用您的代码获得一种实体的数量,只需编辑第二行并添加变量 $cursor=null:

<?php

require __DIR__ . '/vendor/autoload.php';
# Imports the Google Cloud client library
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Datastore\Entity;

$projectId = "__my projectID__";
$datasetId = $projectId;
$datastore = new DatastoreClient(['projectId' => $projectId]);

function getlist($datastore){
$cursor=null;
        $query = $datastore->query()
        ->kind('keypad_research')
        ->start($cursor);

    $results = $datastore->runQuery($query);
    $entries = [];
    $count = 0;
    foreach ($results as $entity) {
        $count++;
    }

    echo $count; // this shows me '0' results even when I have 2 entities.
}
getlist($datastore);
?>

如果您查询的是非默认命名空间,您需要在 initialize your client:

时识别命名空间
$datastore = new DatastoreClient([
    'projectId' => $projectId,
    'namespaceId' => 'my-namespace'
    ]);