1

I am trying to post some form data to Google Datastore, using this documentation here and here. I am struggling with knowing how to post the form data to the datastore, but I thought I had cracked it when I got the following error.

Parse error: syntax error, unexpected 'use' (T_USE) in record-usage.php on line 23

So my question is in two parts:

  1. how do I post data to the Datastore from my form? (the examples in the documentation seem to offer two different ways of posting to the store)
  2. how do I fix this syntax error?

Here is my code (it's actually line 7 in the sample below):

if(isset($_POST['submit'])) {

    # 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 = 'room-usage';

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

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

    # The name/ID for the new entity
    $name = $roomname + $_POST('timestamp');

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

    if($_POST('countinput') == '') {
        $formdata->Headcount = $_POST('countslider');
    } else {
        $formdata->Headcount = $_POST('countinput');
    }
    $formdata->Timestamp = $_POST('timestamp');
    $formdata->EnteredBy = $_POST('userid');
    $formdata->RoomID = $_POST('roomid');
    $formdata->Activity = $_POST('activity');
    $formdata->Estimate = $_POST('estimate');
    $formdata->NotAudited = $_POST('notaudited');
    $formdata->Reason = $_POST('reason');

    function save_room_usage(DatastoreClient $datastore, $formdata) {
    $RoomUsage = $datastore->RoomUsage();

    $transaction->updateBatch([$formdata]);
    $transaction->commit();
    }
    # Prepares the new entity
    $task = $datastore->entity($taskKey, 
                                ['Timestamp' => $_POST('timestamp'),
                                 'Headcount' => $formdata->Headcount,
                                'EnteredBy' => $_POST('userid'),
                                'RoomID' => $_POST('roomid'),
                                'Activity' => $_POST('activity'),
                                'Estimate' => $_POST('estimate'),
                                'NotAudited' => $_POST('notaudited'),
                                'Reason' => $_POST('reason')]);

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

    $status =  'Saved ' . $task->key() . ': ' . $task['description'];


}
Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
  • 1
    Hey Yvonne. Usage documentation for Datastore in Google Cloud PHP can be found here: https://googlecloudplatform.github.io/google-cloud-php/#/docs/cloud-datastore/v0.1.0/datastore/datastoreclient I wrote the client, so if you have any questions that aren't answered in the docs, please let me know! – jdp Mar 14 '17 at 15:49
  • 1
    I also whipped up a quick working example of a working version of your code. It's not 100% how you're using it, but it should give you a template to work off of. Note that in your final application, you should do some input validation to prevent malicious users from taking advantage of your script. That's outside the scope of this example though. https://gist.github.com/jdpedrie/a9973a3835535e9cef13b2fa5b3c6f96 – jdp Mar 14 '17 at 15:58
  • thanks so much for that, really helpful. However, I am getting this error, `Fatal error: Class 'Google\Cloud\Datastore\DatastoreClient' not found in /base/data/home/apps/e~brookes-room-usage/1.399853826395726238/record-usage.php on line 84` so presumably I have failed to point my app correctly to that class (OO PHP is not my strong suit, sorry) – Yvonne Aburrow Mar 15 '17 at 10:01
  • I found the thing about adding a settings.yml file, but I don't know how to find my client ID. – Yvonne Aburrow Mar 15 '17 at 10:38
  • thanks also for your kind offer of answering questions that are not answered in the docs. I have SO MANY questions. I guess the biggest one is, what are the basic prerequisites for using PHP and the Datastore API? Do I need a settings.yml file? What should be in it? where is ti documented? is there an example of it on Github? – Yvonne Aburrow Mar 15 '17 at 13:36
  • and do I need Composer? (I fear that if I asked all of this as a SO question, it would be downvoted or closed as "too broad") – Yvonne Aburrow Mar 15 '17 at 13:43
  • 1
    Yes, you're best installing Google Cloud PHP via composer. The settings.yml file is used to configure appengine for PHP. This guide shows some more about how to get started with appengine: https://cloud.google.com/appengine/docs/flexible/php/quickstart – jdp Mar 15 '17 at 16:43
  • Thanks. I've downloaded this from Github: https://github.com/googlecloudplatform/google-cloud-php and reuploaded it to my app, together with my settings.yml file, but I am getting an error saying that it can't find vendor/autoload.php, which suggests either that the path to it is incorrect (which I don't think it is), or that composer is not installed properly in my app. – Yvonne Aburrow Mar 16 '17 at 11:07
  • You need to run `composer install` before deploying. – jdp Mar 16 '17 at 15:14
  • but I don't want to install composer or PHP on my local machine – Yvonne Aburrow Mar 16 '17 at 16:05

1 Answers1

3

Use

You can't have use inside a function. Think of it like an import.

Instead, move it outside of the method but inside your namespace. Simplest way to get going is just move it to the top of your file.

Writing data

Looking at this code:

$transaction->updateBatch([$formdata]);
$transaction->commit();

I think you've misinterpreted the transaction documentation. In the code example it presents, it first reads entities from Datastore via a Batch Lookup, then writes them back as a batch with:

$transaction->updateBatch([$fromAccount, $toAccount]);

In that example $fromAccount and $toAccount are proper entities with keys.

In your example, you are trying to batch write just a bunch of random data. Since you are only writing one record, you should delete that code.

The second part of your code where you create an entity and then upsert it should work. If you did end up needing to write more than one entity in a transaction, it is $task that you would use in the updateBatch method rather than the raw data.

Community
  • 1
  • 1
Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
  • that worked, thank you. Any idea where I can find some example code for posting data to Datastore? since this other SO post http://stackoverflow.com/questions/19219748/can-google-cloud-datastore-access-with-php/28573652#28573652 , support for PHP has been added, but I didn't find the docs very helpful (see preamble to my post). – Yvonne Aburrow Mar 14 '17 at 14:17