Skip to content

Commit 6370763

Browse files
authored
Merge branch '4.4' into PHPORM-81
2 parents 73527f2 + 7836cad commit 6370763

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
33

44
## [4.4.0] - unreleased
55

6+
* Support collection name prefix by @GromNaN in [#2930](https://github.com/mongodb/laravel-mongodb/pull/2930)
67
* Add `mongodb` driver for Batching by @GromNaN in [#2904](https://github.com/mongodb/laravel-mongodb/pull/2904)
78
* Rename queue option `table` to `collection`
89

src/Connection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function __construct(array $config)
6666
// Select database
6767
$this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config));
6868

69+
$this->tablePrefix = $config['prefix'] ?? '';
70+
6971
$this->useDefaultPostProcessor();
7072

7173
$this->useDefaultSchemaGrammar();
@@ -109,7 +111,7 @@ public function table($table, $as = null)
109111
*/
110112
public function getCollection($name)
111113
{
112-
return new Collection($this, $this->db->selectCollection($name));
114+
return new Collection($this, $this->db->selectCollection($this->tablePrefix . $name));
113115
}
114116

115117
/** @inheritdoc */

tests/ConnectionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public function testConnectionConfig(string $expectedUri, string $expectedDataba
178178

179179
$this->assertSame($expectedUri, (string) $client);
180180
$this->assertSame($expectedDatabaseName, $connection->getMongoDB()->getDatabaseName());
181+
$this->assertSame('foo', $connection->getCollection('foo')->getCollectionName());
182+
$this->assertSame('foo', $connection->collection('foo')->raw()->getCollectionName());
181183
}
182184

183185
public function testConnectionWithoutConfiguredDatabase(): void
@@ -200,6 +202,20 @@ public function testCollection()
200202
$this->assertInstanceOf(Builder::class, $collection);
201203
}
202204

205+
public function testPrefix()
206+
{
207+
$config = [
208+
'dsn' => 'mongodb://127.0.0.1/',
209+
'database' => 'tests',
210+
'prefix' => 'prefix_',
211+
];
212+
213+
$connection = new Connection($config);
214+
215+
$this->assertSame('prefix_foo', $connection->getCollection('foo')->getCollectionName());
216+
$this->assertSame('prefix_foo', $connection->collection('foo')->raw()->getCollectionName());
217+
}
218+
203219
public function testQueryLog()
204220
{
205221
DB::enableQueryLog();

0 commit comments

Comments
 (0)