【问题标题】:Is there a way to only update a single property value with Google Cloud Datastore?有没有办法只使用 Google Cloud Datastore 更新单个属性值?
【发布时间】:2017-11-26 11:57:12
【问题描述】:

我的项目有一个Article 类型。然后,有一个Block 种类。每个Block 都有一篇文章作为父实体;因此,每篇文章都由几个Block 实体组成。每个Block 都有一个订单property,因此最终用户可以订购它们。

在前端,用户可以添加新的Block 或更新现有的。显然,通过将 Block 向上移动,它会改变自己的顺序,但致命的是,它也会改变 (+1) 每个后续的 Block 实体。

到目前为止,对于用户保存的每次更新,我都会检索所有顺序等于或大于新保存的顺序的 Block 实体,并更新它们中的每一个。

有没有办法使用 PHP 库简单地更新一个属性 (order)?

【问题讨论】:

    标签: nosql google-cloud-datastore google-cloud-platform document-database


    【解决方案1】:

    不,您不能只更新实体的属性,您必须更新整个实体。来自Updating an entity

    要更新现有实体,请修改实体的属性 以前使用密钥检索并存储它:

    $transaction = $datastore->transaction();
    $key = $datastore->key('Task', 'sampleTask');
    $task = $transaction->lookup($key);
    $task['priority'] = 5;
    $transaction->upsert($task);
    $transaction->commit();
    

    提供的数据会覆盖现有实体。整个对象 必须发送到 Cloud Datastore。 [...]

    **Note:** To delete a property, remove the property from the entity, then save the entity.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      相关资源
      最近更新 更多