A mutation is a change to the datastore. Create, Update and Delete are
examples of mutations, while Read is not.
Google Cloud Datastore supports multiple mutations in a single API call,
subject to the limits of the service. Adding mutations separately from
committing the changes allows you to create complex operations, both
inside a transaction and not.
public mutation ( string $operation, |
||
$operation | string | The operation to execute. "Insert", "Upsert", "Update" or "Delete". |
$input | The entity or key to mutate. | |
$type | string | The type of the input array. |
$baseVersion | string | [optional] The version of the entity that this mutation is being applied to. If this does not match the current version on the server, the mutation conflicts. |
return | array | [Mutation](https://cloud.google.com/datastore/docs/reference/rest/v1/projects/commit#Mutation). |
/** * Delete multiple records * * No service requests are run when this method is called. * Use {@see Google\Cloud\Datastore\Transaction::commit()} to commit changes. * * Example: * ``` * $keys = [ * $datastore->key('Person', 'Bob'), * $datastore->key('Person', 'John') * ]; * * $transaction->deleteBatch($keys); * $transaction->commit(); * ``` * * @param Key[] $keys The keys to delete. * @return Transaction */ public function deleteBatch(array $keys) { foreach ($keys as $key) { $this->mutations[] = $this->operation->mutation('delete', $key, Key::class); } return $this; }
/** * Delete multiple entities * * Deletion by this method is non-transactional. If you need transaction * support, use {@see Google\Cloud\Datastore\Transaction::deleteBatch()}. * * Example: * ``` * $keys = [ * $datastore->key('Person', 'Bob'), * $datastore->key('Person', 'John') * ]; * * $datastore->deleteBatch($keys); * ``` * * @param Key[] $keys The identifiers to delete. * @param array $options [optional] { * Configuration options * * @type string $baseVersion Provides concurrency control. The version * of the entity that this mutation is being applied to. If this * does not match the current version on the server, the mutation * conflicts. * } * @return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body) */ public function deleteBatch(array $keys, array $options = []) { $options += ['baseVersion' => null]; $mutations = []; foreach ($keys as $key) { $mutations[] = $this->operation->mutation('delete', $key, Key::class, $options['baseVersion']); } return $this->operation->commit($mutations, $options); }