1
votes

I've been unable to locate any documentation for a PHP API for Google Could Datastore. I'm attempting to migrate a NoSQL (MongoDB database) website to Google App Engine - and it appears Cloud Datastore is the best option at present. The only documentation I could locate is for Node.js, Python and Java.

https://developers.google.com/datastore/docs/getstarted/

2
Yeah, I looked there - but no API reference as of yet :( Stuart says something will be available soon: twitter.com/TheFuriousAnt/status/353103115647598592Kenneth Tsang
Meanwhile you can use the API client instead: code.google.com/p/google-api-php-clientMars

2 Answers

0
votes

I have a library for that here: https://github.com/pwhelan/datachore (also available as datachore/datachore from packagist).

0
votes

The official PHP client library from Google is now GA.

You can install it using Composer:

composer require google/cloud

Using it is then as simple as including it, initializing the client for your project, then performing your operations:

# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\Datastore\DatastoreClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$datastore = new DatastoreClient([
    'projectId' => $projectId
]);

# The kind for the new entity
$kind = 'Task';

# The name/ID for the new entity
$name = 'sampletask1';

# The Cloud Datastore key for the new entity
$taskKey = $datastore->key($kind, $name);

# Prepares the new entity
$task = $datastore->entity($taskKey, ['description' => 'Buy milk']);

# Saves the entity
$datastore->upsert($task);

echo 'Saved ' . $task->key() . ': ' . $task['description'] . PHP_EOL;

Previous to the official client library, the most widely used PHP library for Cloud Datastore, that's still used and works, is https://github.com/tomwalder/php-gds

As of version 3, PHP GDS supports the v1 REST API meaning you can use it outside of App Engine on any compute service.