Cloud Datastore PHP返回0个结果

时间:2018-11-20 07:03:02

标签: google-cloud-datastore

因此,我尝试首先查看我的PHP是否可以与数据存储区对话并检索数据。 我创建了两个实体,类型为“ 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从数据存储中读取数据的最基本尝试失败了。

任何建议都值得赞赏。

2 个答案:

答案 0 :(得分:1)

使用您的代码,我已经能够获得一种实体的数量,仅编辑第二行并添加变量$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);
?>

答案 1 :(得分:1)

如果要查询非默认名称空间,则在initialize your client时需要标识名称空间:

$datastore = new DatastoreClient([
    'projectId' => $projectId,
    'namespaceId' => 'my-namespace'
    ]);