diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eff79c84..500f1ee46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## [4.3.1] * Fix memory leak when filling nested fields using dot notation by @GromNaN in [#2962](https://github.com/mongodb/laravel-mongodb/pull/2962) +* Fix PHP error when accessing the connection after disconnect by @SanderMuller in [#2967](https://github.com/mongodb/laravel-mongodb/pull/2967) ## [4.3.0] - 2024-04-26 diff --git a/src/Connection.php b/src/Connection.php index 0c5015489..ec337d524 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -208,7 +208,7 @@ public function ping(): void /** @inheritdoc */ public function disconnect() { - unset($this->connection); + $this->connection = null; } /** diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 262c4cafc..60ee9ee3f 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -38,6 +38,22 @@ public function testReconnect() $this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2)); } + public function testDisconnectAndCreateNewConnection() + { + $connection = DB::connection('mongodb'); + $this->assertInstanceOf(Connection::class, $connection); + $client = $connection->getMongoClient(); + $this->assertInstanceOf(Client::class, $client); + $connection->disconnect(); + $client = $connection->getMongoClient(); + $this->assertNull($client); + DB::purge('mongodb'); + $connection = DB::connection('mongodb'); + $this->assertInstanceOf(Connection::class, $connection); + $client = $connection->getMongoClient(); + $this->assertInstanceOf(Client::class, $client); + } + public function testDb() { $connection = DB::connection('mongodb');