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 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
Parameters | |
---|---|
Name | Description |
connection |
Connection\ConnectionInterface
A connection to Cloud Firestore. |
valueMapper |
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);
Parameters | |
---|---|
Name | Description |
document |
DocumentReference
The document to retrieve. |
options |
array
Configuration options. |
Returns | |
---|---|
Type | Description |
DocumentSnapshot |
runAggregateQuery
Get an Aggregate Query Snapshot.
Example:
$snapshot = $transaction->runAggregateQuery($aggregateQuery);
Parameters | |
---|---|
Name | Description |
aggregateQuery |
AggregateQuery
The aggregate query to retrieve. |
options |
array
Configuration Options |
↳ readTime |
Timestamp
Reads entities as they were at the given timestamp. |
Returns | |
---|---|
Type | Description |
AggregateQuerySnapshot |
documents
See also:
Parameters | |
---|---|
Name | Description |
paths |
string[]|array<DocumentReference>
Any combination of string paths or DocumentReference instances. |
options |
array
Configuration options. |
Returns | |
---|---|
Type | Description |
array<DocumentSnapshot> |
runQuery
Run a Query inside the Transaction.
Example:
$results = $transaction->runQuery($query);
Parameters | |
---|---|
Name | Description |
query |
Query
A Firestore Query. |
options |
array
Configuration options. |
Returns | |
---|---|
Type | Description |
QuerySnapshot |
create
Enqueue an operation to create a Firestore document.
Example:
$transaction->create($document, [
'name' => 'John',
'country' => 'USA'
]);
Parameters | |
---|---|
Name | Description |
document |
DocumentReference
The document to create. |
fields |
array
An array containing fields, where keys are the field names, and values are field values. Nested arrays are allowed. Note that unlike DocumentReference::update(), field paths are NOT supported by this method. |
Returns | |
---|---|
Type | Description |
Transaction |
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
]);
Parameters | |
---|---|
Name | Description |
document |
DocumentReference
The document to modify or replace. |
fields |
array
An array containing fields, where keys are the field names, and values are field values. Nested arrays are allowed. Note that unlike 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 |
Returns | |
---|---|
Type | Description |
Transaction |
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 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
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">FieldPath</xref> will properly escape each element.
use Google\Cloud\Firestore\FieldPath;
$transaction->update($document, [
['path' => new FieldPath(['cryptoCurrencies', 'big$$$coin']), 'value' => 5.51]
]);
Parameters | |
---|---|
Name | Description |
document |
DocumentReference
The document to modify or replace. |
data |
array[]
A list of arrays of form
|
options |
array
Configuration options |
Returns | |
---|---|
Type | Description |
Transaction |
delete
Enqueue an operation to delete a Firestore document.
Example:
$transaction->delete($document);
Parameters | |
---|---|
Name | Description |
document |
DocumentReference
The document to delete. |
options |
array
Configuration Options. |
Returns | |
---|---|
Type | Description |
Transaction |
writer
Get the BulkWriter object.
Returns | |
---|---|
Type | Description |
BulkWriter |