Skip to content

Commit 27cc06b

Browse files
committed
PHPORM-82 Support prefix for collection names
1 parent f93aaa7 commit 27cc06b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: src/Connection.php

+3-1
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 */

Diff for: tests/ConnectionTest.php

+16
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)