PHP Class Google\Cloud\Datastore\Transaction

A transaction is a set of Datastore operations on one or more entities. Each transaction is guaranteed to be atomic, which means that transactions are never partially applied. Either all of the operations in the transaction are applied, or none of them are applied. It is highly recommended that users read and understand the underlying concepts in Transactions before beginning. Mutations (i.e. insert, update and delete) are not executed immediately. Calls to those methods (and their batch equivalents) will enqueue a new mutation. Calling {@see \Google\Cloud\Datastore\Transaction::commit()} will execute all the mutationsin the order they were enqueued, and end the transaction. Lookups and queries can be run in a transaction, so long as they are run prior to committing or rolling back the transaction. Transactions are an **optional** feature of Google Cloud Datastore. Queries, lookups and mutations can be executed outside of a Transaction from {@see \Google\Cloud\Datastore\DatastoreClient}. Example: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $datastore = $cloud->datastore(); $transaction = $datastore->transaction();
See also: https://cloud.google.com/datastore/docs/concepts/transactions Transactions
Datei anzeigen Open project: GoogleCloudPlatform/gcloud-php

Public Methods

Method Description
__construct ( Operation $operation, string $projectId, string $transactionId ) Create a Transaction
commit ( array $options = [] ) : array Commit all mutations
delete ( Key $key ) : Transaction Delete a record
deleteBatch ( array $keys ) : Transaction Delete multiple records
insert ( Entity $entity ) : Transaction Insert an entity
insertBatch ( array $entities ) : Transaction Insert multiple entities
lookup ( Key $key, array $options = [] ) : Entity | null Retrieve an entity from the datastore inside a transaction
lookupBatch ( array $keys, array $options = [] ) : array Get multiple entities inside a transaction
rollback ( ) : void Roll back a Transaction
runQuery ( Google\Cloud\Datastore\Query\QueryInterface $query, array $options = [] ) : Generator Run a query and return entities inside a Transaction
update ( Entity $entity, array $options = [] ) : Transaction Update an entity
updateBatch ( array $entities, array $options = [] ) : Transaction Update multiple entities
upsert ( Entity $entity ) : Transaction Upsert an entity
upsertBatch ( array $entities ) : Transaction Upsert multiple entities

Method Details

__construct() public method

Create a Transaction
public __construct ( Operation $operation, string $projectId, string $transactionId )
$operation Operation Class that handles shared API interaction.
$projectId string The Google Cloud Platform project ID.
$transactionId string The transaction to run mutations in.

commit() public method

Calling this method will end the operation (and close the transaction, if one is specified). Example: $transaction->commit();
public commit ( array $options = [] ) : array
$options array [optional] Configuration Options.
return array [Response Body](https://cloud.google.com/datastore/reference/rest/v1/projects/commit#response-body)

delete() public method

No service requests are run when this method is called. Use {@see \Google\Cloud\Datastore\Transaction::commit()} to commit changes. Example: $key = $datastore->key('Person', 'Bob'); $transaction->delete($key); $transaction->commit();
public delete ( Key $key ) : Transaction
$key Key The key to delete
return Transaction

deleteBatch() public method

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();
public deleteBatch ( array $keys ) : Transaction
$keys array The keys to delete.
return Transaction

insert() public method

No service requests are run when this method is called. Use {@see \Google\Cloud\Datastore\Transaction::commit()} to commit changes. Example: $key = $datastore->key('Person', 'Bob'); $entity = $datastore->entity($key, ['firstName' => 'Bob']); $transaction->insert($entity); $transaction->commit();
public insert ( Entity $entity ) : Transaction
$entity Entity The entity to insert.
return Transaction

insertBatch() public method

No service requests are run when this method is called. Use {@see \Google\Cloud\Datastore\Transaction::commit()} to commit changes. Example: $entities = [ $datastore->entity('Person', ['firstName' => 'Bob']), $datastore->entity('Person', ['firstName' => 'John']) ]; $transaction->insertBatch($entities); $transaction->commit();
public insertBatch ( array $entities ) : Transaction
$entities array The entities to insert.
return Transaction

lookup() public method

Example: $key = $datastore->key('Person', 'Bob'); $entity = $transaction->lookup($key); if (!is_null($entity)) { echo $entity['firstName']; // 'Bob' }
public lookup ( Key $key, array $options = [] ) : Entity | null
$key Key $key The identifier to use to locate a desired entity.
$options array [optional] { Configuration Options @type string $className The name of the class to return results as. Must be a subclass of {@see \Google\Cloud\Datastore\Entity}. If not set, {@see \Google\Cloud\Datastore\Entity} will be used. }
return Entity | null

lookupBatch() public method

Example: $keys = [ $datastore->key('Person', 'Bob'), $datastore->key('Person', 'John') ]; $entities = $transaction->lookupBatch($keys); foreach ($entities['found'] as $entity) { echo $entity['firstName'] . PHP_EOL; }
public lookupBatch ( array $keys, array $options = [] ) : array
$keys array
$options array [optional] { Configuration Options @type string|array $className If a string, the name of the class to return results as. Must be a subclass of {@see \Google\Cloud\Datastore\Entity}. If not set, {@see \Google\Cloud\Datastore\Entity} will be used. If an array is given, it must be an associative array, where the key is a Kind and the value is the name of a subclass of {@see \Google\Cloud\Datastore\Entity}. @type bool $sort If set to true, results in each set will be sorted to match the order given in $keys. **Defaults to** `false`. }
return array Returns an array with keys [`found`, `missing`, and `deferred`]. Members of `found` will be instance of {@see \Google\Cloud\Datastore\Entity}. Members of `missing` and `deferred` will be instance of {@see \Google\Cloud\Datastore\Key}.

rollback() public method

Example: $transaction->rollback();
public rollback ( ) : void
return void

runQuery() public method

Example: $result = $transaction->runQuery($query); foreach ($result as $entity) { echo $entity['firstName']; }
public runQuery ( Google\Cloud\Datastore\Query\QueryInterface $query, array $options = [] ) : Generator
$query Google\Cloud\Datastore\Query\QueryInterface The query object.
$options array [optional] { Configuration Options @type string $className The name of the class to return results as. Must be a subclass of {@see \Google\Cloud\Datastore\Entity}. If not set, {@see \Google\Cloud\Datastore\Entity} will be used. }
return Generator

update() public method

No service requests are run when this method is called. Use {@see \Google\Cloud\Datastore\Transaction::commit()} to commit changes. Example: $entity['firstName'] = 'Bob'; $transaction->update($entity); $transaction->commit();
public update ( Entity $entity, array $options = [] ) : Transaction
$entity Entity The entity to update.
$options array [optional] { Configuration Options @type bool $allowOverwrite Entities must be updated as an entire resource. Patch operations are not supported. Because entities can be created manually, or obtained by a lookup or query, it is possible to accidentally overwrite an existing record with a new one when manually creating an entity. To provide additional safety, this flag must be set to `true` in order to update a record when the entity provided was not obtained through a lookup or query. **Defaults to** `false`. }
return Transaction

updateBatch() public method

No service requests are run when this method is called. Use {@see \Google\Cloud\Datastore\Transaction::commit()} to commit changes. Example: $entities[0]['firstName'] = 'Bob'; $entities[1]['firstName'] = 'John'; $transaction->updateBatch($entities); $transaction->commit();
public updateBatch ( array $entities, array $options = [] ) : Transaction
$entities array The entities to update.
$options array [optional] { Configuration Options @type bool $allowOverwrite Entities must be updated as an entire resource. Patch operations are not supported. Because entities can be created manually, or obtained by a lookup or query, it is possible to accidentally overwrite an existing record with a new one when manually creating an entity. To provide additional safety, this flag must be set to `true` in order to update a record when the entity provided was not obtained through a lookup or query. **Defaults to** `false`. }
return Transaction

upsert() public method

No service requests are run when this method is called. Use {@see \Google\Cloud\Datastore\Transaction::commit()} to commit changes. Upsert will create a record if one does not already exist, or overwrite existing record if one already exists. Example: $key = $datastore->key('Person', 'Bob'); $entity = $datastore->entity($key, ['firstName' => 'Bob']); $transaction->upsert($entity); $transaction->commit();
public upsert ( Entity $entity ) : Transaction
$entity Entity The entity to upsert.
return Transaction

upsertBatch() public method

No service requests are run when this method is called. Use {@see \Google\Cloud\Datastore\Transaction::commit()} to commit changes. Upsert will create a record if one does not already exist, or overwrite existing record if one already exists. Example: $keys = [ $datastore->key('Person', 'Bob'), $datastore->key('Person', 'John') ]; $entities = [ $datastore->entity($keys[0], ['firstName' => 'Bob']), $datastore->entity($keys[1], ['firstName' => 'John']) ]; $transaction->upsertBatch($entities); $transaction->commit();
public upsertBatch ( array $entities ) : Transaction
$entities array The entities to upsert.
return Transaction