duanlie2709 2014-12-25 05:20
浏览 33
已采纳

如何在php中为GAE数据存储生成实体ID?

i am trying to insert new entity using PHP client library into datastore, i am using datastore_connect.php file from this example, https://github.com/amygdala/appengine_php_datastore_example

I want to insert entity with auto id, not the name. I see that there is function setId(), but i dont know how to generate proper id. Whats the best practice in doing so?

Thanks

function createKeyForTestItem () {
    $path = new Google_Service_Datastore_KeyPathElement();
    $path->setKind("testkind");
    $path->setName("testkeyname");
    //$path->setId(??)
    $key = new Google_Service_Datastore_Key();
    $key->setPath([$path]);
    return $key;
}
  • 写回答

2条回答 默认 最新

  • 普通网友 2014-12-30 18:20
    关注

    You can have Cloud Datastore generate the ID for you by populating the insertAutoId field on the mutation instead of the upsert field.

    Here's a code snippet (adapted from the datastore_connect.php file you posted):

    function create_key() {
      $path = new Google_Service_Datastore_KeyPathElement();
      $path->setKind("testkind");
      // Neither name nor ID is set.
      $key = new Google_Service_Datastore_Key();
      $key->setPath([$path]);
      return $key;
    }
    function create_entity() {
      $entity = new Google_Service_Datastore_Entity();
      $entity->setKey(create_key());
      // Add properties...
      return $entity;
    }
    function create_commit_request() {
      $entity = create_entity();
      $mutation = new Google_Service_Datastore_Mutation();
      $mutation->setInsertAutoId([$entity]);  // Causes ID to be allocated.
      $req = new Google_Service_Datastore_CommitRequest();
      $req->setMode('NON_TRANSACTIONAL');
      $req->setMutation($mutation);
      return $req;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?