# Cloud Firestore Client - Class Transaction (2.2.0-RC1)

2.2.0-RC1 2.1.0 (latest) 2.0.2-RC1 1.55.0 1.54.4 1.53.1 1.52.0 1.51.0 1.50.0 1.48.1 1.47.3 1.46.0 1.45.2 1.44.1 1.43.3 1.40.0 1.39.0 1.38.0 1.37.7 1.36.1 1.35.0 1.34.0 1.33.0 1.32.0 1.31.0 1.30.0 1.28.0 1.27.3 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()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/FirestoreClient#_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 \\ Firestore

## Methods

### __construct

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `firestoreClient` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/V1.Client.FirestoreClient` A FirestoreClient instance. |
| `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` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference` The document to retrieve. |
| `options` | `array` Configuration options. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentSnapshot` |   |

### runAggregateQuery

Get an Aggregate Query Snapshot.

Example:

    $snapshot = $transaction->runAggregateQuery($aggregateQuery);

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `aggregateQuery` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/AggregateQuery` The aggregate query to retrieve. |
| `options` | `array` Configuration Options |
| `↳ readTime` | `Timestamp` Reads entities as they were at the given timestamp. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/AggregateQuerySnapshot` |   |

### documents

See also:

- [BatchGetDocuments](https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1beta1#google.firestore.v1beta1.Firestore.BatchGetDocuments)

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `paths` | `string[]|array<https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference>` Any combination of string paths or DocumentReference instances. |
| `options` | `array` Configuration options. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `array<https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentSnapshot>` |   |

### runQuery

Run a Query inside the Transaction.

Example:

    $results = $transaction->runQuery($query);

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `query` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/Query` A Firestore Query. |
| `options` | `array` Configuration options. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/QuerySnapshot` |   |

### create

Enqueue an operation to create a Firestore document.

Example:

    $transaction->create($document, [
        'name' => 'John',
        'country' => 'USA'
    ]);

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `document` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference#_Google_Cloud_Firestore_DocumentReference__update__), field paths are NOT supported by this method. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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()](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/Transaction#_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`. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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](https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/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` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference` The document to modify or replace. |
| `data` | `array[]` A list of arrays of form `[FieldPath|string $path, mixed $value]`. |
| `options` | `array` Configuration options |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/Transaction` |   |

### delete

Enqueue an operation to delete a Firestore document.

Example:

    $transaction->delete($document);

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `document` | `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/DocumentReference` The document to delete. |
| `options` | `array` Configuration Options. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/Transaction` |   |

### writer

Get the BulkWriter object.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-firestore/latest/BulkWriter` |   |