---
title: "Session: Saving changes"
url: "https://docs.ravendb.net/6.2/client-api/session/saving-changes"
version: "6.2"
language: "php"
---

# Session: Saving changes

> Client language: PHP. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/saving-changes.md), [Java](https://docs.ravendb.net/6.2/client-api/session/saving-changes.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/saving-changes.python.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/saving-changes.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

Pending session operations like `store`, `delete`, and many others, will not be sent to the server until `saveChanges` is called.

> **Info**
>
> When `saveChanges()` is applied to send a batch of operations (e.g. `put`, `update`, and `delete`)
> in a request, the server will wrap these operations in a [transaction](https://docs.ravendb.net/6.2/client-api/faq/transaction-support.md)
> upon execution in the database.
>
> Either all operations are performed as a single, atomic transaction, or none of them are.  
> Once `saveChanges()` returns successfully, it is guaranteed that all changes are persisted in the database.

## Syntax

```php
public function saveChanges(): void;
```

### Example

```php
$employee = new Employee();
$employee->setFirstName("John");
$employee->setLastName("Doe");

$session->store($employee);
$session->saveChanges();;
```

## Waiting for Indexes

You can request the server to wait until the indexes are caught up with changes made within
the current session before `saveChanges` returns.

- You can set a timeout (default: 15 seconds).
- You can specify whether you want to throw on timeout (default: `false`).
- You can specify indexes that you want to wait for. If you don't specify anything here, RavenDB will automatically select just the indexes that are impacted
  by this write.

```php
$session->advanced()->waitForIndexesAfterSaveChanges(function ($builder) use ($session) {
  $builder->withTimeout(Duration::ofSeconds(30))
      ->throwOnTimeout(true)
      ->waitForIndexes("index/1", "index/2");

  $employee = new Employee();
  $employee->setFirstName("John");
  $employee->setLastName("Doe");

  $session->store($employee);

  $session->saveChanges();
});
```

## Transaction Mode - Cluster Wide

Setting `TransactionMode` to `TransactionMode.clusterWide` will enable the [Cluster Transactions](https://docs.ravendb.net/6.2/server/clustering/cluster-transactions.md) feature.

With this feature enabled the [session](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.php.md) will support the following *write* commands:

- `store`
- `delete`
- `createCompareExchangeValue`
- `updateCompareExchangeValue`
- `deleteCompareExchangeValue`

Here is an example of creating a unique user with cluster wide.

```php
$session = $store->openSession();
try {
  $sessionOptions = new SessionOptions();
  // default is: TransactionMode::singleNode();
  $sessionOptions->setTransactionMode(TransactionMode::clusterWide());
  $session = $store->openSession($sessionOptions);
  try {
      $user = new Employee();
      $user->setFirstName("John");
      $user->setLastName("Doe");

      $session->store($user);

      // this transaction is now conditional on this being
      // successfully created (so, no other users with this name)
      // it also creates an association to the new user's id
      $session->advanced()->clusterTransaction()
          ->createCompareExchangeValue("usernames/John", $user->getId());

      $session->saveChanges();
  } finally {
      $session->close();
  }
} finally {
  $store->close();
}
```

## See also

- [Transaction Support](https://docs.ravendb.net/6.2/client-api/faq/transaction-support.md)
- [Cluster Transactions](https://docs.ravendb.net/6.2/server/clustering/cluster-transactions.md)
- [What is a Session and How Does it Work](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.php.md)
- [Opening a Session](https://docs.ravendb.net/6.2/client-api/session/opening-a-session.php.md)
- [Deleting Entities](https://docs.ravendb.net/6.2/client-api/session/deleting-entities.php.md)
- [Loading Entities](https://docs.ravendb.net/6.2/client-api/session/loading-entities.php.md)
- [Query Overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.php.md)
- [Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.php.md)
- [What is a Document Store](https://docs.ravendb.net/6.2/client-api/what-is-a-document-store.md)
