Skip to content

Commit bb58fcb

Browse files
committed
Pass connection instance to transactional closure
This is consistent with the behaviour of the original ManagesTransactions concern.
1 parent 2a82a54 commit bb58fcb

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/Concerns/ManagesTransactions.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [
8484
$attemptsLeft = $attempts;
8585
$callbackResult = null;
8686

87-
$session = $this->getSessionOrCreate();
88-
8987
$callbackFunction = function (Session $session) use ($callback, &$attemptsLeft, &$callbackResult) {
9088
$attemptsLeft--;
9189

@@ -95,10 +93,10 @@ public function transaction(Closure $callback, $attempts = 1, array $options = [
9593
return;
9694
}
9795

98-
$callbackResult = $callback();
96+
$callbackResult = $callback($this);
9997
};
10098

101-
with_transaction($session, $callbackFunction, $options);
99+
with_transaction($this->getSessionOrCreate(), $callbackFunction, $options);
102100

103101
return $callbackResult;
104102
}

tests/TransactionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Support\Facades\DB;
4+
use Jenssegers\Mongodb\Connection;
45
use Jenssegers\Mongodb\Eloquent\Model;
56
use MongoDB\BSON\ObjectId;
67
use MongoDB\Driver\Server;
@@ -316,7 +317,7 @@ public function testTransaction(): void
316317
{
317318
User::create(['name' => 'klinson', 'age' => 20, 'title' => 'admin']);
318319

319-
DB::transaction(function (): void {
320+
DB::transaction(function (Connection $connection): void {
320321
User::create(['name' => 'alcaeus', 'age' => 38, 'title' => 'admin']);
321322
User::where(['name' => 'klinson'])->update(['age' => 21]);
322323
});

0 commit comments

Comments
 (0)