Skip to content

Instantly share code, notes, and snippets.

@jdpedrie
Last active March 15, 2017 09:52
Show Gist options
  • Save jdpedrie/a9973a3835535e9cef13b2fa5b3c6f96 to your computer and use it in GitHub Desktop.
Save jdpedrie/a9973a3835535e9cef13b2fa5b3c6f96 to your computer and use it in GitHub Desktop.
<?php
use Google\Cloud\Datastore\DatastoreClient;
$datastore = new DatastoreClient([
'projectId' => $projectId
]);
// I aliased $_POST, but you don't need to if you don't want to.
// This is where I'd perform all the validation and sanitation on the data that was given by the user.
$inputData = $_POST;
// Make sure you're setting $roomName somewhere.
$roomName = 'foo';
$name = $roomName . $inputData['timestamp'];
// Create a key with a name.
$key = $datastore->key('RoomUsage', $name);
// We'll create our entity, give it the key we just created, then fill it with data from the form.
$roomUsage = $datastore->entity($key, [
'Headcount' => ($inputData['countinput'] === '')
? $inputData['countslider']
: $inputData['countinput'],
'Timestamp' => $inputData['timestamp'],
'EnteredBy' => $inputData['userid'],
'RoomID' => $inputData['roomid'],
'Activiy' => $inputData['activity'],
'Estimate' => $inputData['estimate'],
'NotAudited' => $inputData['notaudited'],
'Reason' => $inputData['reason']
]);
// Finally, send the entity to Datastore.
$datastore->upsert($roomUsage);
@vogelbeere
Copy link

vogelbeere commented Mar 15, 2017

Thank you so much for this. It is a thing of beauty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment