Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tristansokol/3fe98b6d5a8635b148f55ec123e480ed to your computer and use it in GitHub Desktop.
Save tristansokol/3fe98b6d5a8635b148f55ec123e480ed to your computer and use it in GitHub Desktop.
Datastore php cursor example
//my cursor is set as a url variable ($_GET['albumoffset'])
$albumStore = new \GDS\Store('Tracks');
$obj_store->query("SELECT * FROM Albums");
$obj_store->setCursor(rawurldecode($_GET['albumoffset']));
$entity = $obj_store->fetchPage(1);
//do stuff with my new entity
//meta refresh the page with the new cursor.
echo '<meta http-equiv="refresh" content="0; url=http://'.$_SERVER['HTTP_HOST'].'/dev/migrate?albumoffset='.rawurlencode($obj_store->getCursor()).'">';
@phpso10
Copy link

phpso10 commented Jul 25, 2016

How do I pass the Cursor variable to $entity = $obj_store->fetchPage(1);
?

@phpso10
Copy link

phpso10 commented Jul 26, 2016

If I Use the below code it errors on SetCursor line.

$obj_schema_2 = new GDS\Schema('channels');
$obj_gateway_ns_2 = new GDS\Gateway\ProtoBuf(null, 'bkdatastore');
$obj_store_ns_2 = new \GDS\Store($obj_schema_2, $obj_gateway_ns_2);
$results= $obj_store_ns_2->query("SELECT * FROM channels where createdby='" . $str_bkuserid . "'" );
$obj_store_ns_2->setCursor("cursor-1");

ERROR _>

HP Fatal error: Uncaught exception 'google\appengine\runtime\ApplicationError' with message 'com.google.protobuf.InvalidProtocolBufferException: Error parsing protocol message' in /base/data/home/runtimes/php/sdk/google/appengine/runtime/RealApiProxy.php:53
Stack trace:
#0 /base/data/home/runtimes/php/sdk/google/appengine/runtime/ApiProxy.php(40): google\appengine\runtime\RealApiProxy->makeSyncCall('datastore_v4', 'RunQuery', Object(google\appengine\datastore\v4\RunQueryRequest), Object(google\appengine\datastore\v4\RunQueryResponse), 60)
#1 /base/data/home/apps/sbottlekast-1001/2.394464971199658891/api/datastore/src/GDS/Gateway/ProtoBuf.php(213): google\appengine\runtime\ApiProxy::makeSyncCall('datastore_v4', 'RunQuery', Object(google\appengine\datastore\v4\RunQueryRequest), Object(google\appengine\datastore\v4\RunQueryResponse), 60)
#2 /base/data/home/apps/s
bottlekast-1001/2.394464971199658891/api/datastore/src/GDS/Gateway/ProtoBuf.php(299): GDS\Gateway\ProtoBuf->execute('RunQuery', Object(google\appengine\datastore\v4\R in /base/data/home/runtimes/php/sdk/google/appengine/runtime/RealApiProxy.php on line 53

@phpso10
Copy link

phpso10 commented Jul 26, 2016

I could come this far only... but don't know where and how to pass the Cursor variable to FetchPage.

$obj_schema = new GDS\Schema('channels');
$obj_gateway_ns = new GDS\Gateway\ProtoBuf(null, 'bkdatastore');
$obj_store_ns = new \GDS\Store($obj_schema, $obj_gateway_ns);

     $str_gql = "SELECT * FROM channels";
    $obj_request = getBasicFetchRequest();
   $obj_gql_query = $obj_request->mutableGqlQuery();
    $obj_gql_query->setAllowLiteral(TRUE);
     $obj_gql_query->setQueryString($str_gql . " LIMIT @intPageSize OFFSET @startCursor");

    $obj_arg = $obj_gql_query->addNameArg();
    $obj_arg->setName('intPageSize');
    $obj_arg->mutableValue()->setIntegerValue(100);

     $obj_arg_offset = $obj_gql_query->addNameArg();
    $obj_arg_offset->setName('startCursor');
    $obj_arg_offset->setCursor('cur1');

$arr_result = $obj_store_ns->query($str_gql)->fetchPage(5);

 //   $arr_result = $obj_store_ns->fetchPage(5);

     foreach ($arr_result as $obj_ch)
  {
    echo $obj_ch->title, PHP_EOL;
    }

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