In primo luogo, installare l'API ufficiale di Google Cloud Datastore PHP utilizzando Composer (un gestore di dipendenza). Si può fare questo con l'aggiunta di "google/cloud-datastore": "^1.0"
alla sezione 'require' della vostra composer.json e funzionante composer update
è possibile avviare l'emulatore Datastore locale con il comando:
gcloud beta emulators datastore start
Ecco una classe di supporto che ho scritto gestisce la connessione al 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;
}
}
Ecco come si dovrebbe utilizzare per inserire una nuova entità nel 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);
Se si verificano problemi con l'emulatore, provare a distribuire il codice su App Engine e verificare se si ha lo stesso errore. L'ambiente di sviluppo locale può essere instabile.
Inoltre, controlla il official PHP Datastore API docs here.
fonte
2017-09-05 15:51:11
possibile duplicato di [Il runtime GAE PHP rilasciato di recente può accedere al datastore GAE nativo?] (Http://stackoverflow.com/questions/16601074/can-the-recently-released-gae-php-runtime-access- the-native-gae-datastore) –
sì, hai ragione, è una duplicazione, ma è perché non c'è una risposta completa sull'altro post. Hai detto "È stato aggiunto il supporto al più recente client php API di google - Guarda la classe Google_DatastoreService. Proverò a fare un breve tutorial dimostrativo presto." il 15 agosto e ora è il 25 novembre e non ci sono ancora tutorial disponibili :(Ecco perché la gente continua a fare la stessa domanda: il mio progetto di gioco è stato interrotto solo perché non so come usare Datastore in PHP su GAE. tutorial su come possiamo archiviare e recuperare un oggetto stile JSON in Datastore –