GAE PHP数据存储区查询

时间:2015-03-15 22:00:50

标签: php google-app-engine google-cloud-datastore

对于我正在使用Google App Engine的数据存储区和PHP的项目,该数据库没有官方文档。

我使用以下指南成功地将新实体添加到数据存储区,但现在我正在努力使查询工作,以便我可以检索数据并将其显示在我的网页上。 https://gae-php-tips.appspot.com/2013/12/23/getting-started-with-the-cloud-datastore-on-php-app-engine/

这是我目前的代码:

try {
// test the config and connectivity by creating a test entity, building
// a commit request for that entity, and creating/updating it in the datastore
//  $req = createRequest();
//  $service_dataset->commit($dataset_id, $req, []);

    $req = createQuery();
//  printQueryResults($req);

}
catch (Google_Exception $ex) {
 syslog(LOG_WARNING, 'Commit to Cloud Datastore exception: ' . $ex->getMessage());
 echo "There was an issue -- check the logs.";
 return;
}

function createQuery()
{
    $gql_query = new Google_Service_Datastore_GqlQuery();
    $gql_query->setQueryString("SELECT * FROM 'Notes' WHERE name = 'test1'");
    $gql_query->setAllowLiteral(true);

    $req = new Google_Service_Datastore_RunQueryRequest();
    $req->setGqlQuery($gql_query);

    return $req;
}

我希望能够查询我的数据存储区并获取所有具有匹配名称的实体。

2 个答案:

答案 0 :(得分:1)

我成功测试了以下代码,我假设您使用的是您提到的指南中的DatastoreService.php。必须有不同的方法来解析查询结果,但这里有一个;)

config.php:替换您的凭据

<?php
    $google_api_config = [
      'application-id' => 'xxxxxxxxxxxxxxx',
      'service-account-name' => 'xxxxx@developer.gserviceaccount.com',
      'private-key' => file_get_contents('xxxxxxx.p12'),
      'dataset-id' => 'xxxxxxxxxxxx'
    ];

你的代码改编了

require_once 'config.php';
require_once 'DatastoreService.php';

try {
    $req = createQuery();
}
catch (Google_Exception $ex) {
 syslog(LOG_WARNING, 'Commit to Cloud Datastore exception: ' . $ex->getMessage());
 echo "There was an issue -- check the logs.";
 return;
}
// from config.php
$options = $google_api_config;
$datastoreService = new DatastoreService($options);

$result = $datastoreService->runQuery($req, $optParams = []);
$results = $result->getBatch()->getEntityResults();

$items = array();
foreach ($results as $item) {
    $item = $item->getEntity()->getProperties();
    $items[] = $item['name']['stringValue'];
}
echo '<plaintext>' . print_r($items, true);

function createQuery()
{
    $gql_query = new Google_Service_Datastore_GqlQuery();
    $gql_query->setQueryString($query = "SELECT * FROM Notes WHERE name = 'test1'");
    $gql_query->setAllowLiteral(true);

    $req = new Google_Service_Datastore_RunQueryRequest();
    $req->setGqlQuery($gql_query);

    return $req;
}

答案 1 :(得分:0)

通过composer添加google/cloud-datastore库,如下所示。

$ composer require google/cloud-datastore

您可以使用Query mothod作为示例。

<?php 
require 'vendor/autoload.php';

use Google\Cloud\Datastore\DatastoreClient;

$datastore = new DatastoreClient([
    'projectId' => 'my_project'
]);


$query = $datastore->query();
$query->kind('Notes');
$query->filter('name ', '=', 'test1');

$res = $datastore->runQuery($query);
foreach ($res as $notes) {
    echo $notes['name']; // test1
}

或者可以使用query object

进行构建
<?php 
$query = $datastore->query([
    'query' => [
        'kind' => [
            [
                'name' => 'Notes'
            ]
        ],
        'filter' => [
            'propertyFilter' => [
                'op' => 'EQUAL',
                'property' => [
                    'name' => 'name'
                ],
                'value' => [
                    'stringValue' => 'test1'
                ]
            ]
        ]
    ]
]);