Home » Php » How to write to local App Engine datastore with PHP

How to write to local App Engine datastore with PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

What is the best way to develop locally in PHP and the Google datastore?

The dev_appserver.py docs say I can view local datastore entities, but there is no documentation on how to connect/write to this local datastore using PHP.

I can write to the local datastore emulator using:

// Start emulator: gcloud beta emulators datastore start --data-dir=_datastore
// Pointing this to dev_appserver's 'API server' doesn't work.
putenv('DATASTORE_EMULATOR_HOST=http://localhost:8081');
$datastore = $cloud->datastore();

But these entities do not show up in dev_appserver.py’s local admin server at http://localhost:8000/datastore.

Even setting the dev_appserver’s --datastore_path to be equal to the emulator’s --data-dir does nothing.

Why are the datastore emulator and dev_appserver.py’s datastore different? They share the same name and the docs refer to them interchangeably. This is frustrating.

Is this the correct way to do local datastore development? Is there a way to write to the local datastore and have the entities show up in the admin server viewer?

How to&Answers:

Google Cloud Datastore Emulator and dev_appserver.py have different underlying storage.
So the entities on datastore emulator cannot be shown in the admin server viewer.

see:

There are maybe two way to connect to local datasotre (I’m not tried with php):

  1. Use appengine-php-sdk for standard enviroment, start development server by dev_appserver.py, and view datastore from admin server viewer.

  2. Use google/cloud library for php, and set DATASTORE_EMULATOR_HOST env variable.

Unfortunately there isn’t viewer for emulator, so I’m developing cli tool to check datastore entities by GQL: https://github.com/nshmura/dsio

Answer:

have u tried passing the environment variables to the php executable before including the libraries?

maybe try this:

#/bin/sh
export DATASTORE_EMULATOR_HOST=http://localhost:8081
dev_appserver.py ...