【问题标题】:How to access Google Cloud datastore with php?如何使用 php 访问 Google Cloud 数据存储区?
【发布时间】:2013-10-07 08:05:34
【问题描述】:

我正在为我的网络应用程序使用 Google 应用引擎,并且我需要使用 NoSQL 数据库,所以我最好的选择是 Google Cloud Datastore

由于我找不到将它与 php 连接的方法,我无法使用它。在official documentation php 中没有提到。我想确保有办法使用 php 访问它吗?

【问题讨论】:

  • 是的,你是对的,这是重复的,但这是因为其他帖子没有完整的答案。您说“最新的 google php api 客户端已添加支持 - 查看 Google_DatastoreService 类。我会尽快尝试做一个快速演示教程。” 8 月 15 日,现在是 11 月 25 日,仍然没有可用的教程 :( 这就是为什么人们一直问同样的问题。我的游戏项目停止了,因为我不知道如何在 GAE 上使用 PHP 中的 Datastore。请提供一个简单的关于如何在 Datastore 中存储和检索 JSON 样式对象的教程。

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


【解决方案1】:

这个库可以帮助人们找到这个线程

旨在通过 PHP 轻松使用 Google Datatore。

https://github.com/tomwalder/php-gds

【讨论】:

  • 你能解释一下如何在没有作曲家的情况下进行设置吗?
  • 这个库现在是 2.0 beta 并且有更好的文档。 Composer 仍然是首选的安装机制。
  • 此外,它现在通过库附带的内置解析器提供本地 GQL 支持。我希望有人觉得它有用! github.com/tomwalder/php-gds
【解决方案2】:

没有记录从 PHP 访问 Google Cloud Datastore,但您应该能够像使用任何其他 Google API 一样使用Google APIs Client Library for PHP

【讨论】:

    【解决方案3】:
    【解决方案4】:

    首先,使用Composer(依赖管理器)安装官方的 Google Cloud Datastore PHP API。您可以通过将 "google/cloud-datastore": "^1.0" 添加到 composer.json 的“require”部分并运行 composer update

    来做到这一点

    您可以使用以下命令启动本地 Datastore 模拟器:

    gcloud beta emulators datastore start
    

    这是我编写的一个帮助程序类,用于处理与数据存储的连接:

    <?php
    // Datastore.php
    use Google\Cloud\Datastore\DatastoreClient;
    
    class Datastore {
        private static $ds;
    
        /**
         * Creates or returns the Datastore instance
         *
         * @return void
         */
        public static function getOrCreate() {
            if (isset(Datastore::$ds)) return Datastore::$ds;
    
            // gcloud beta emulators datastore start --data-dir=_datastore
            if (Datastore::isDevEnv() == true) {
                putenv('DATASTORE_EMULATOR_HOST=http://localhost:8081');
                // To run locally, you may still need to download a credentials file from console.cloud.google.com
                //putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/datastore_creds.json');            
            }
    
            $datastore = new DatastoreClient([
                'projectId' => Core::getProjectId()  
                // 'keyFilePath' => 'datastore_creds.json'
            ]);
    
            Datastore::$ds = $datastore;
            return Datastore::$ds;
        }
    
        /**
         * Returns true if server running in development environment.
         *
         * @return boolean
         */
        static function isDevEnv() {
            if (isset(Core::$_isDevEnv)) return Core::$_isDevEnv;
            Core::$_isDevEnv = (strpos(getenv('SERVER_SOFTWARE'), 'Development') === 0);
            return Core::$_isDevEnv;
        }
    
        /**
         * Formats fields and indexes for datastore.
         * @param Datastore $ds
         * @param Key $entityKey Datastore key.
         * @param [] $fields
         * @param [string] $indexes Keys to index.
         * @return Entity
         */
        static function entityWithIndexes(DatastoreClient $ds, $entityKey, $fields, $indexes = []) {
            // Create exclude from indexes array.
            $excludedIndexes = [];
            foreach ($fields as $key => $value) {
                if (in_array($key, $indexes) == false) {
                    $excludedIndexes[] = $key;
                }
            }
    
            $entity = $ds->entity($entityKey, $fields, [
                'excludeFromIndexes' => $excludedIndexes
            ]);
            return $entity;
        }
    }
    

    这是您将如何使用它将新实体插入数据存储区的方法

    require 'vendor/autoload.php';
    require 'Datastore.php';    
    $ds = Datastore::getOrCreate();
    $key = $ds->key('MyEntityType');
    $data = [
          'name' => 'Foo!'
    ];
    $indexes = ['name'];
    $entity = Datastore::entityWithIndexes($ds, $key, $data, $indexes);
    $ds->insert($entity);
    

    如果您在使用模拟器时遇到问题,请尝试将您的代码部署到 App Engine 并查看是否有同样的错误。本地开发环境可能不稳定。

    另外,请查看official PHP Datastore API docs here

    【讨论】:

      【解决方案5】:

      通过composer 包含google/cloud-datastore

      $ composer require google/cloud-datastore

      你可以使用下面的例子。

      <?php 
      require 'vendor/autoload.php';
      
      use Google\Cloud\Datastore\DatastoreClient;
      
      $datastore = new DatastoreClient([
          'projectId' => 'my_project'
      ]);
      
      // Create an entity
      $bob = $datastore->entity('Person');
      $bob['firstName'] = 'Bob';
      $bob['email'] = 'bob@example.com';
      $datastore->insert($bob);
      
      // Update the entity
      $bob['email'] = 'bobV2@example.com';
      $datastore->update($bob);
      
      // If you know the ID of the entity, you can look it up
      $key = $datastore->key('Person', '12345328897844');
      $entity = $datastore->lookup($key);
      

      更多详情:https://github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-datastore-ga

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-07
        • 2018-03-31
        • 2018-12-23
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 2019-06-12
        • 2017-12-23
        相关资源
        最近更新 更多