Open a Session
-
A Session object is obtained from the Document Store.
-
Open a session using
openSession()
. -
Various Session options can be configured using the
sessionOptions
object.
If no database is specified in the options then the Default Database (stored in the Document Store) is assumed. -
Make sure you release allocated session resources using the
close
method or wrapping the session object in atry
statement (see below). -
In this page:
Syntax
- Use
openSession()
to open a session from the Document Store. - The following overloads are available:
// Open session for a 'default' database configured in 'DocumentStore'
public function openSession(): DocumentSessionInterface;
// Open session for a specified database
public function openSession(string $database): DocumentSessionInterface;
public function openSession(SessionOptions $sessionOptions): DocumentSessionInterface;
Parameter | Type | Description |
---|---|---|
database | string |
The Session will operate on this database, overriding the default database set in the document store. |
options | sessionOptions |
An object with Session configuration options. See details below. |
Return Value | Description |
---|---|
DocumentSessionInterface |
Document session interface |
Session options
- The
sessionOptions
object contains various options to configure the Session's behavior.
Option | Type | Description | Default Value |
---|---|---|---|
Database | string |
The Session will operate on this database, overriding the Default Database. |
null - the Session operates on the Default Database |
NoTracking | bool |
true - The Session tracks changes made to all entities it loaded, stored, or queried for.false - Tracking will be turned off.Learn more in Disable tracking |
false |
NoCaching | bool |
true - Server responses will Not be cached.false - The Session caches the server responses.Learn more in Disable caching |
false |
RequestExecutor | RequestExecutor |
( Advanced option ) The request executor the Session should use. |
null - the default request executor is used |
TransactionMode | TransactionMode |
Specify the Session's transaction modeSingleNode / ClusterWide Learn more in Cluster-wide vs. Single-node |
SingleNode |
- Experts Only:
Option | Type | Description | Default Value |
---|---|---|---|
DisableAtomicDocumentWrites- InClusterWideTransaction |
bool? |
( Experts only ) true - Disable Atomic-Guards in cluster-wide sessions.false - Automatic atomic writes in cluster-wide sessions are enabled.Learn more in Atomic-Guards |
false |
Open session examples
Example 1
$store->openSession(new SessionOptions());
Example 2
$sessionOptions = new SessionOptions();
$sessionOptions->setDatabase($databaseName);
$store->openSession($sessionOptions);
Example 3 - Releasing session resources
Always remember to release allocated session resources after usage by
invoking the close
method or wrapping the session object in a try
statement.
$session = $store->openSession();
try {
// code here
} finally {
$session->close();
}
Example 4 - Disable entities tracking
$sessionOptions = new SessionOptions();
$sessionOptions->setNoTracking(true);
$session = $store->openSession();
try {
$employee1 = $session->load(Employee::class, "employees/1-A");
$employee2 = $session->load(Employee::class, "employees/1-A");
// because NoTracking is set to 'true'
// each load will create a new Employee instance
$this->assertNotSame($employee1, $employee2);
} finally {
$session->close();
}
Example 5 - Disable session caching
$sessionOptions = new SessionOptions();
$sessionOptions->setNoCaching(true);
$session = $store->openSession();
try {
// code here
} finally {
$session->close();
}