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.
});
An array containing fields, where keys are the field
names, and values are field values. Nested arrays are allowed.
Note that unlikeDocumentReference::update(), field
paths are NOT supported by this method.
An array containing fields, where keys are the field
names, and values are field values. Nested arrays are allowed.
Note that unlikeTransaction::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 tofalse.
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 inFieldValue.
Note that field names must be provided using field paths, encoded either
as a dot-delimited string (i.e.foo.bar), or an instance ofFieldPath. 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, patha.bcannot be set directly if pathais also
provided.
// 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]
]);
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Firestore Client - Class Transaction (1.53.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.53.0 (latest)](/php/docs/reference/cloud-firestore/latest/Transaction)\n- [1.52.0](/php/docs/reference/cloud-firestore/1.52.0/Transaction)\n- [1.51.0](/php/docs/reference/cloud-firestore/1.51.0/Transaction)\n- [1.50.0](/php/docs/reference/cloud-firestore/1.50.0/Transaction)\n- [1.48.1](/php/docs/reference/cloud-firestore/1.48.1/Transaction)\n- [1.47.3](/php/docs/reference/cloud-firestore/1.47.3/Transaction)\n- [1.46.0](/php/docs/reference/cloud-firestore/1.46.0/Transaction)\n- [1.45.2](/php/docs/reference/cloud-firestore/1.45.2/Transaction)\n- [1.44.1](/php/docs/reference/cloud-firestore/1.44.1/Transaction)\n- [1.43.3](/php/docs/reference/cloud-firestore/1.43.3/Transaction)\n- [1.40.0](/php/docs/reference/cloud-firestore/1.40.0/Transaction)\n- [1.39.0](/php/docs/reference/cloud-firestore/1.39.0/Transaction)\n- [1.38.0](/php/docs/reference/cloud-firestore/1.38.0/Transaction)\n- [1.37.7](/php/docs/reference/cloud-firestore/1.37.7/Transaction)\n- [1.36.1](/php/docs/reference/cloud-firestore/1.36.1/Transaction)\n- [1.35.0](/php/docs/reference/cloud-firestore/1.35.0/Transaction)\n- [1.34.0](/php/docs/reference/cloud-firestore/1.34.0/Transaction)\n- [1.33.0](/php/docs/reference/cloud-firestore/1.33.0/Transaction)\n- [1.32.0](/php/docs/reference/cloud-firestore/1.32.0/Transaction)\n- [1.31.0](/php/docs/reference/cloud-firestore/1.31.0/Transaction)\n- [1.30.0](/php/docs/reference/cloud-firestore/1.30.0/Transaction)\n- [1.28.0](/php/docs/reference/cloud-firestore/1.28.0/Transaction)\n- [1.27.3](/php/docs/reference/cloud-firestore/1.27.3/Transaction) \nReference documentation and code samples for the Cloud Firestore Client class Transaction.\n\nRepresents a Firestore transaction.\n\nThis class should be accessed inside a transaction callable, obtained via\n[FirestoreClient::runTransaction()](/php/docs/reference/cloud-firestore/latest/FirestoreClient#_Google_Cloud_Firestore_FirestoreClient__runTransaction__).\n\nNote that method examples, while shown as being called directly for the sake\nof brevity, should be called only within the context of a transaction\ncallable, as noted above.\n\nExample: \n\n use Google\\Cloud\\Firestore\\FirestoreClient;\n use Google\\Cloud\\Firestore\\Transaction;\n\n $firestore = new FirestoreClient();\n $document = $firestore-\u003edocument('users/john');\n $firestore-\u003erunTransaction(function (Transaction $transaction) use ($document) {\n // Manage Transaction.\n });\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Firestore\n\nMethods\n-------\n\n### __construct\n\n### snapshot\n\nGet a Document Snapshot.\n\nExample: \n\n $snapshot = $transaction-\u003esnapshot($document);\n\n### runAggregateQuery\n\nGet an Aggregate Query Snapshot.\n\nExample: \n\n $snapshot = $transaction-\u003erunAggregateQuery($aggregateQuery);\n\n### documents\n\nSee also:\n\n- [BatchGetDocuments](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.Firestore.BatchGetDocuments)\n\n### runQuery\n\nRun a Query inside the Transaction.\n\nExample: \n\n $results = $transaction-\u003erunQuery($query);\n\n### create\n\nEnqueue an operation to create a Firestore document.\n\nExample: \n\n $transaction-\u003ecreate($document, [\n 'name' =\u003e 'John',\n 'country' =\u003e 'USA'\n ]);\n\n### set\n\nEnqueue an operation to modify or replace a Firestore document.\n\nExample: \n\n // In this example, all field not explicitly specified will be removed.\n $transaction-\u003eset($document, [\n 'name' =\u003e 'Johnny'\n ]);\n\n // To specify MERGE over REPLACE, set `$options.merge` to `true`.\n $transaction-\u003eset($document, [\n 'name' =\u003e 'Johnny'\n ], [\n 'merge' =\u003e true\n ]);\n\n### update\n\nEnqueue an update with field paths and values.\n\nMerges provided data with data stored in Firestore.\n\nCalling this method on a non-existent document will raise an exception.\n\nThis method supports various sentinel values, to perform special operations\non fields. Available sentinel values are provided as methods, found in\n[FieldValue](/php/docs/reference/cloud-firestore/latest/FieldValue).\n\nNote that field names must be provided using field paths, encoded either\nas a dot-delimited string (i.e. `foo.bar`), or an instance of\n[FieldPath](/php/docs/reference/cloud-firestore/latest/FieldPath). Nested arrays are not allowed.\n\nPlease note that conflicting paths will result in an exception. Paths\nconflict when one path indicates a location nested within another path.\nFor instance, path `a.b` cannot be set directly if path `a` is also\nprovided.\n\nExample: \n\n $transaction-\u003eupdate($document, [\n ['path' =\u003e 'name', 'value' =\u003e 'John'],\n ['path' =\u003e 'country', 'value' =\u003e 'USA'],\n ['path' =\u003e 'cryptoCurrencies.bitcoin', 'value' =\u003e 0.5],\n ['path' =\u003e 'cryptoCurrencies.ethereum', 'value' =\u003e 10],\n ['path' =\u003e 'cryptoCurrencies.litecoin', 'value' =\u003e 5.51]\n ]);\n\n // Google Cloud PHP provides special field values to enable operations such\n // as deleting fields or setting the value to the current server timestamp.\n use Google\\Cloud\\Firestore\\FieldValue;\n\n $transaction-\u003eupdate($document, [\n ['path' =\u003e 'country', 'value' =\u003e FieldValue::deleteField()],\n ['path' =\u003e 'lastLogin', 'value' =\u003e FieldValue::serverTimestamp()]\n ]);\n\n // If your field names contain special characters (such as `.`, or symbols),\n // using \u003cxref uid=\"\\Google\\Cloud\\Firestore\\FieldPath\"\u003eFieldPath\u003c/xref\u003e will properly escape each element.\n\n use Google\\Cloud\\Firestore\\FieldPath;\n\n $transaction-\u003eupdate($document, [\n ['path' =\u003e new FieldPath(['cryptoCurrencies', 'big$$$coin']), 'value' =\u003e 5.51]\n ]);\n\n### delete\n\nEnqueue an operation to delete a Firestore document.\n\nExample: \n\n $transaction-\u003edelete($document);\n\n### writer\n\nGet the BulkWriter object."]]