【问题标题】:GAE PHP Datastore QueryGAE PHP 数据存储查询
【发布时间】:2015-03-15 22:00:49
【问题描述】:

对于一个项目,我使用 Google App Engine 的 Datastore 和 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;
}

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

【问题讨论】:

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


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      通过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'
                      ]
                  ]
              ]
          ]
      ]); 
      

      【讨论】:

        猜你喜欢
        • 2013-06-21
        • 2018-08-12
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        • 2013-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多