2015-03-15 2 views
0

Для проекта я использую Datastore Google App Engine с PHP, у которого нет официальной документации.GAE PHP Datastore Query

Я использовал следующее руководство, чтобы успешно добавлять новые объекты в хранилище данных, но теперь я изо всех сил стараюсь получить запросы, чтобы я мог получать данные и отображать их на своей веб-странице. 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; 
} 

Я хочу, чтобы иметь возможность запрашивать мой хранилищу и получить все объекты, которые имеют совпадающее имя.

ответ

1

Я успешно тестировал этот следующий код, я предполагаю, что вы используете DatastoreService.php из упомянутого вами руководства. Там должны быть разные способы разбора результата запроса, но здесь один;)

config.php: заменить с учетными данными

<?php 
    $google_api_config = [ 
     'application-id' => 'xxxxxxxxxxxxxxx', 
     'service-account-name' => '[email protected]', 
     '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; 
}