Lazhar
Lazhar

Reputation: 1458

Is there a way to only update a single property value with Google Cloud Datastore?

My project has an Article kind. Then, there is a Block kind. Each Block has an article as parent entity; therefore, each article is made up of several Block entities. Each Block has an order property so the end-user can order them.

On the frontend, users can add a new Block or update an existing one. Obviously, by moving a Block up, it changes its own order but fatally, it also changes (+1) each of the subsequent Block entities.

So far, for every update the user saves, I retrieve all Block entities with an order equal or greater than the newly saved one, and update each of them.

Is there are way to simply update one property (order) using the PHP library?

Upvotes: 1

Views: 885

Answers (1)

Dan Cornilescu
Dan Cornilescu

Reputation: 39824

No, you cannot update just a property of an entity, you have to update the entire entity. From Updating an entity:

To update an existing entity, modify the properties of the entity previously retrieved and store it using the key:

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

The provided data overwrites the existing entity. The entire object must be sent to Cloud Datastore. [...]

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

Upvotes: 1

Related Questions