Reference documentation and code samples for the Cloud Firestore Client class Transaction.
Represents a Firestore transaction.
This class should be accessed inside a transaction callable, obtained via Google\Cloud\Firestore\FirestoreClient::runTransaction() .
Note that method examples, while shown as being called directly for the sake of brevity, should be called only within the context of a transaction callable, as noted above.
Example:
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\Transaction;
$firestore = new FirestoreClient();
$document = $firestore->document('users/john');
$firestore->runTransaction(function (Transaction $transaction) use ($document) {
// Manage Transaction.
});
Namespace
Google \ Cloud \ FirestoreMethods
__construct
connection
Google\Cloud\Firestore\Connection\ConnectionInterface
A connection to Cloud Firestore.
valueMapper
Google\Cloud\Firestore\ValueMapper
A Firestore Value Mapper.
database
string
The database name.
transaction
string
The transaction ID.
snapshot
Get a Document Snapshot.
Example:
$snapshot = $transaction->snapshot($document);
document
options
array
Configuration options.
runAggregateQuery
Get an Aggregate Query Snapshot.
Example:
$snapshot = $transaction->runAggregateQuery($aggregateQuery);
aggregateQuery
options
array
Configuration Options
↳ readTime
Timestamp
Reads entities as they were at the given timestamp.
documents
See also:
paths
string[]|array< Google\Cloud\Firestore\DocumentReference
>
Any combination of string paths or DocumentReference instances.
options
array
Configuration options.
runQuery
Run a Query inside the Transaction.
Example:
$results = $transaction->runQuery($query);
query
options
array
Configuration options.
create
Enqueue an operation to create a Firestore document.
Example:
$transaction->create($document, [
'name' => 'John',
'country' => 'USA'
]);
document
fields
array
An array containing fields, where keys are the field names, and values are field values. Nested arrays are allowed. Note that unlike Google\Cloud\Firestore\DocumentReference::update() , field paths are NOT supported by this method.
set
Enqueue an operation to modify or replace a Firestore document.
Example:
// In this example, all field not explicitly specified will be removed.
$transaction->set($document, [
'name' => 'Johnny'
]);
// To specify MERGE over REPLACE, set `$options.merge` to `true`.
$transaction->set($document, [
'name' => 'Johnny'
], [
'merge' => true
]);
document
fields
array
An array containing fields, where keys are the field names, and values are field values. Nested arrays are allowed. Note that unlike Google\Cloud\Firestore\Transaction::update() , field paths are NOT supported by this method.
options
array
Configuration options.
↳ merge
bool
If true, unwritten fields will be preserved. Otherwise, they will be overwritten (removed). Defaults to false
.
update
Enqueue an update with field paths and values.
Merges provided data with data stored in Firestore.
Calling this method on a non-existent document will raise an exception.
This method supports various sentinel values, to perform special operations on fields. Available sentinel values are provided as methods, found in Google\Cloud\Firestore\FieldValue .
Note that field names must be provided using field paths, encoded either
as a dot-delimited string (i.e. foo.bar
), or an instance of Google\Cloud\Firestore\FieldPath
. Nested arrays are not allowed.
Please note that conflicting paths will result in an exception. Paths
conflict when one path indicates a location nested within another path.
For instance, path a.b
cannot be set directly if path a
is also
provided.
Example:
$transaction->update($document, [
['path' => 'name', 'value' => 'John'],
['path' => 'country', 'value' => 'USA'],
['path' => 'cryptoCurrencies.bitcoin', 'value' => 0.5],
['path' => 'cryptoCurrencies.ethereum', 'value' => 10],
['path' => 'cryptoCurrencies.litecoin', 'value' => 5.51]
]);
// Google Cloud PHP provides special field values to enable operations such
// as deleting fields or setting the value to the current server timestamp.
use Google\Cloud\Firestore\FieldValue;
$transaction->update($document, [
['path' => 'country', 'value' => FieldValue::deleteField()],
['path' => 'lastLogin', 'value' => FieldValue::serverTimestamp()]
]);
// If your field names contain special characters (such as `.`, or symbols),
// using <xref uid="\Google\Cloud\Firestore\FieldPath">Google\Cloud\Firestore\FieldPath</xref> will properly escape each element.
use Google\Cloud\Firestore\FieldPath;
$transaction->update($document, [
['path' => new FieldPath(['cryptoCurrencies', 'big$$$coin']), 'value' => 5.51]
]);
document
data
array[]
A list of arrays of form [FieldPath|string $path, mixed $value]
.
options
array
Configuration options
delete
Enqueue an operation to delete a Firestore document.
Example:
$transaction->delete($document);
document
options
array
Configuration Options.
writer
Get the BulkWriter object.