Skip to content

Commit 0d84061

Browse files
committed
Merge pull request #477
2 parents 2036f03 + cfdfcb1 commit 0d84061

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
arg_name: option
2+
name: causalConsistency
3+
type: boolean
4+
description: |
5+
Enables or disables :ref:`causal consistency <causal-consistency>` for the
6+
session. If true, each operation in the session will be causally ordered after
7+
the previous read or write operation. Set to false to disable causal
8+
consistency. Defaults to true.
9+
interface: phpmethod
10+
operation: ~
11+
optional: true
12+
...

docs/reference/class/MongoDBClient.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ Methods
3939
/reference/method/MongoDBClient-listDatabases
4040
/reference/method/MongoDBClient-selectCollection
4141
/reference/method/MongoDBClient-selectDatabase
42+
/reference/method/MongoDBClient-startSession
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
===============================
2+
MongoDB\\Client::startSession()
3+
===============================
4+
5+
.. default-domain:: mongodb
6+
7+
.. versionadded:: 1.3
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\Client::startSession()
19+
20+
Start a new client session for use with this client.
21+
22+
.. code-block:: php
23+
24+
function startSession(array $options = []): MongoDB\Driver\Session
25+
26+
The ``$options`` parameter supports the following options:
27+
28+
.. include:: /includes/apiargs/MongoDBClient-method-startSession-option.rst
29+
30+
Return Values
31+
-------------
32+
33+
A :php:`MongoDB\Driver\Session <mongodb-driver-session>`
34+
35+
Errors/Exceptions
36+
-----------------
37+
38+
.. include:: /includes/extracts/error-driver-invalidargumentexception.rst
39+
.. include:: /includes/extracts/error-driver-runtimeexception.rst
40+
41+
Example
42+
-------
43+
44+
The following example starts a new session:
45+
46+
.. code-block:: php
47+
48+
<?php
49+
50+
$client = new MongoDB\Client;
51+
52+
$session = $client->startSession();
53+
54+
var_dump($session);
55+
56+
The output would then resemble::
57+
58+
object(MongoDB\Driver\Session)#2043 (4) {
59+
["logicalSessionId"]=>
60+
array(1) {
61+
["id"]=>
62+
object(MongoDB\BSON\Binary)#225 (2) {
63+
["data"]=>
64+
string(16) "................"
65+
["type"]=>
66+
int(4)
67+
}
68+
}
69+
["clusterTime"]=>
70+
NULL
71+
["causalConsistency"]=>
72+
bool(true)
73+
["operationTime"]=>
74+
NULL
75+
}
76+
77+
See Also
78+
--------
79+
80+
- :php:`MongoDB\\Driver\\Manager::startSession()
81+
<manual/en/mongodb-driver-manager.startsession.php>`
82+
- :ref:`Causal Consistency <causal-consistency>` in the MongoDB manual

src/Client.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,16 @@ public function selectDatabase($databaseName, array $options = [])
256256

257257
return new Database($this->manager, $databaseName, $options);
258258
}
259+
260+
/**
261+
* Start a new client session.
262+
*
263+
* @see http://php.net/manual/en/mongodb-driver-manager.startsession.php
264+
* @param array $options Session options
265+
* @return MongoDB\Driver\Session
266+
*/
267+
public function startSession(array $options = [])
268+
{
269+
return $this->manager->startSession($options);
270+
}
259271
}

tests/ClientFunctionalTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,12 @@ private function assertDatabaseExists($databaseName, $callback = null)
9797
call_user_func($callback, $foundDatabase);
9898
}
9999
}
100+
101+
public function testStartSession()
102+
{
103+
if (version_compare($this->getFeatureCompatibilityVersion(), '3.6', '<')) {
104+
$this->markTestSkipped('startSession() is only supported on FCV 3.6 or higher');
105+
}
106+
$this->assertInstanceOf('MongoDB\Driver\Session', $this->client->startSession());
107+
}
100108
}

0 commit comments

Comments
 (0)