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). |
public function mutation($operation, $input, $type, $baseVersion = null)
{
// If the given element is an Entity, it will use that baseVersion.
if ($input instanceof Entity) {
$baseVersion = $input->baseVersion();
$data = $this->entityMapper->objectToRequest($input);
} elseif ($input instanceof Key) {
$data = $input->keyObject();
} else {
throw new InvalidArgumentException(sprintf('Input must be a Key or Entity, %s given', get_class($input)));
}
return array_filter([$operation => $data, 'baseVersion' => $baseVersion]);
}
/** * 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; }