6

I am using Google app engine for my web app and I need to use a NoSQL database, so my best option is Google Cloud Datastore

Since I can't find a way to connect it with php I can't use it. In the official documentation php is not mentioned. I want to make sure that is there a way to access it with php ?

N S
  • 2,524
  • 6
  • 32
  • 42
sha375420
  • 139
  • 1
  • 9
  • possible duplicate of [Can the recently released GAE PHP runtime access the native GAE datastore?](http://stackoverflow.com/questions/16601074/can-the-recently-released-gae-php-runtime-access-the-native-gae-datastore) – Stuart Langley Oct 07 '13 at 09:26
  • yeah, you're right, it's a duplication, but it's because there is no complete answer on the other post. You said "There's support been added to the latest google php api client - Look at the class Google_DatastoreService. I'll try and do a quick demo tutorial soon." on August 15th and now it's November 25th and there is still no tutorial available :( That's why people keep asking the same question. My game project's stopped just because I don't know how to use Datastore in PHP on GAE. Please provide a simple tutorial on how we can store and retrieve a JSON style object in Datastore. – Behnam Rasooli Nov 24 '13 at 14:30

5 Answers5

6

This library may help people finding this thread

Designed to make Google Datatore easy from PHP.

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

Tom
  • 1,563
  • 12
  • 12
  • 1
    Can you please explain how to set it up without composer ? – Rookie Mar 16 '15 at 16:55
  • The library is in 2.0 beta now and has better documentation. Composer is still the preferred install mechanism. – Tom Jul 08 '15 at 22:49
  • Plus it now provides local GQL support through a built in parser shipped with the library. I hope someone finds it useful! https://github.com/tomwalder/php-gds – Tom Jul 09 '15 at 07:27
1

Accessing Google Cloud Datastore from PHP is not documented, but you should be able to use Google APIs Client Library for PHP like any other Google APIs.

proppy
  • 10,495
  • 5
  • 37
  • 66
1

https://gae-php-tips.appspot.com/2013/12/24/getting-started-with-the-cloud-datastore-on-php-app-engine-part-ii/

A good starter primer for php access to datastore

1

First, install the official Google Cloud Datastore PHP API using Composer (a dependency manager). You can do this by adding "google/cloud-datastore": "^1.0" to the 'require' section of your composer.json and running composer update

You can start the local Datastore emulator with the command:

gcloud beta emulators datastore start

Here's a helper class I wrote that handles connecting to the datastore:

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

Here's how you would use it to insert a new entity into the datastore

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

If you run into issues with the emulator, try deploying your code to App Engine and seeing if you have the same error. The local development environment can be flaky.

Also, check out the official PHP Datastore API docs here.

N S
  • 2,524
  • 6
  • 32
  • 42
0

Include google/cloud-datastore library via composer

$ composer require google/cloud-datastore

and you can use as Example below.

<?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);

More details: https://github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-datastore-ga

Bala
  • 913
  • 9
  • 18