1212use MongoDB \Driver \Exception \RuntimeException ;
1313use MongoDB \Driver \ReadConcern ;
1414use MongoDB \Driver \ReadPreference ;
15+ use MongoDB \Driver \Session as DriverSession ;
1516use MongoDB \Driver \WriteConcern ;
1617use MongoDB \Driver \WriteResult ;
1718use Yii ;
@@ -85,7 +86,10 @@ class Command extends BaseObject
8586 * @var ReadConcern|string read concern to be used by this command
8687 */
8788 private $ _readConcern ;
88-
89+ /**
90+ * @var DriverSession session to be used by this command
91+ */
92+ private $ _session ;
8993
9094 /**
9195 * Returns read preference for this command.
@@ -167,6 +171,17 @@ public function setReadConcern($readConcern)
167171 return $ this ;
168172 }
169173
174+ /**
175+ * Sets session for this command.
176+ * @param DriverSession $session session
177+ * @return $this self reference
178+ */
179+ public function setSession ($ session )
180+ {
181+ $ this ->_session = $ session ;
182+ return $ this ;
183+ }
184+
170185 /**
171186 * Executes this command.
172187 * @return \MongoDB\Driver\Cursor result cursor.
@@ -183,7 +198,11 @@ public function execute()
183198
184199 $ this ->db ->open ();
185200 $ mongoCommand = new \MongoDB \Driver \Command ($ this ->document );
186- $ cursor = $ this ->db ->manager ->executeCommand ($ databaseName , $ mongoCommand , $ this ->getReadPreference ());
201+ $ options = array_filter ([
202+ 'readPreference ' => $ this ->getReadPreference (),
203+ 'session ' => $ this ->_session ,
204+ ]);
205+ $ cursor = $ this ->db ->manager ->executeCommand ($ databaseName , $ mongoCommand , $ options );
187206 $ cursor ->setTypeMap ($ this ->db ->typeMap );
188207
189208 $ this ->endProfile ($ token , __METHOD__ );
@@ -236,7 +255,11 @@ public function executeBatch($collectionName, $options = [])
236255 }
237256
238257 $ this ->db ->open ();
239- $ writeResult = $ this ->db ->manager ->executeBulkWrite ($ databaseName . '. ' . $ collectionName , $ batch , $ this ->getWriteConcern ());
258+ $ options = array_filter ([
259+ 'writeConcern ' => $ this ->getWriteConcern (),
260+ 'session ' => $ this ->_session ,
261+ ]);
262+ $ writeResult = $ this ->db ->manager ->executeBulkWrite ($ databaseName . '. ' . $ collectionName , $ batch , $ options );
240263
241264 $ this ->endProfile ($ token , __METHOD__ );
242265 } catch (RuntimeException $ e ) {
@@ -283,7 +306,11 @@ public function query($collectionName, $options = [])
283306
284307 $ query = new \MongoDB \Driver \Query ($ this ->document , $ options );
285308 $ this ->db ->open ();
286- $ cursor = $ this ->db ->manager ->executeQuery ($ databaseName . '. ' . $ collectionName , $ query , $ this ->getReadPreference ());
309+ $ options = array_filter ([
310+ 'readPreference ' => $ this ->getReadPreference (),
311+ 'session ' => $ this ->_session ,
312+ ]);
313+ $ cursor = $ this ->db ->manager ->executeQuery ($ databaseName . '. ' . $ collectionName , $ query , $ options );
287314 $ cursor ->setTypeMap ($ this ->db ->typeMap );
288315
289316 $ this ->endProfile ($ token , __METHOD__ );
0 commit comments