Simple How-To : PHP CRUD from Datastore on GAE

  1. Register your GAE/GCP Account
  2. Download php-gds class from Tom Walder PHP-GDS
  3. Use/Copy :
    • Folder : src/GDS/
    • File : examples/_includes.php
  4. Your Directory Structure Should be :
    src/GDS/
    _includes.php
    app.yaml
    set_vars.cmd
    index.php
    create.php
    read.php
    update.php
    delete.php
  5. Remove pre-path url require_once function so your _includes.php content became :
    require_once('src/GDS/Entity.php');
    ...
    ...
    require_once('src/GDS/Exception/GQL.php');
    
  6. Your app.yaml should be :
    runtime: php55
    threadsafe: true
    
    handlers:
    - url: /(.+\.php)$
      script: \1
    
    - url: /
      script: index.php
    
  7. For Local Datastore Emulator, perhaps you need to configure set_vars.cmd file :
    set DATASTORE_DATASET=pakdhe-001
    set DATASTORE_EMULATOR_HOST=localhost:8081
    set DATASTORE_EMULATOR_HOST_PATH=localhost:8081/datastore
    set DATASTORE_HOST=http://localhost:8081
    set DATASTORE_PROJECT_ID=pakdhe-001
    
  1. Your PHP Script create.php will be :
    require_once('_includes.php');
    
    $obj_store = new \GDS\Store('Book');
    
    // Create a simple Entity object
    $obj_book = $obj_store->createEntity([
    	'title' => 'Bastian Tito',
    	'author' => 'Wiro Sableng Jilid 2',
    	'isbn' => '0812036387',
    	'location'=>'Jakarta'
    ]);
    
    // Insert into the Datastore
    $obj_store->upsert($obj_book);
    
    // Show the key
    echo "Created: ", $obj_book->getKeyId();
    
  2. Your PHP Script read.php will be :
    require_once('_includes.php');
    
    $obj_store = new \GDS\Store('Book');
    $sql = "SELECT * FROM Book ORDER BY author ASC";
    $arr_posts = $obj_store->query($sql)->fetchAll();
    
    print_r($arr_posts);
    
  3. Your PHP Script update.php will be :
    require_once('_includes.php');
    
    $obj_store = new \GDS\Store('Book');
    //eg the ID : 
    $id = '1234567890';
    //get the record :
    $obj = $obj_store->fetchById($id);
    		
    //assign new datastore value;
    //or choose custom field
    $obj->title = "Wiro Sableng : Kapak Maut Naga Geni";
    $obj->author = "Bastian Tito";
    $obj->isbn = "123123";
    $obj->location = "Banda Aceh";
    
    // Insert into the Datastore
    $obj_store->upsert($obj);
    
    
  4. Your PHP Script delete.php will be :
    require_once('_includes.php');
    
    $obj_store = new \GDS\Store('Book');
    
    //eg the ID : 
    $id = '5066549580791808';
    
    //get the record :
    $obj = $obj_store->fetchById($id);
    //delete the record
    $obj_store->delete($obj);
    
    

 Last Step

Running your localhost emulator http://localhost:8080 by :
#_>dev_appserver.py app.yaml -A 123

And check your local emulator datastore kind by accessing :
http://localhost:8000/datastore

or.. Deploy your app to cloud by :
#_>gcloud app deploy