0
votes

For a project I am using Google App Engine's Datastore with PHP, which does not have official documentation.

I used the following guide to successfully be able to add new entities to the datastore, but now I am struggling to get queries working so that I can retrieve data and display it on my web page. https://gae-php-tips.appspot.com/2013/12/23/getting-started-with-the-cloud-datastore-on-php-app-engine/

Here is my current code:

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;
}

I want to be able to query my datastore and get all the entities that have a matching name.

2

2 Answers

1
votes

I tested successfully this following code adapted, i assume that you are using DatastoreService.php from the guide you mentioned. There must be different ways to parse the query result but here is one ;)

config.php : replace with your credentials

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

your code adapted

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;
}
0
votes

Include google/cloud-datastore library via composer as below.

$ composer require google/cloud-datastore

and you can use Query mothod as Example below.

<?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
}

Or it can be build using query object

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