Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PHPORM-82 Support prefix for collection names #2930

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## [4.3.0] - unreleased
## [4.4.0] - unreleased

* Support collection name prefix by @GromNaN in [#2930](https://github.com/mongodb/laravel-mongodb/pull/2930)

## [4.3.0] - 2024-04-26

* New aggregation pipeline builder by @GromNaN in [#2738](https://github.com/mongodb/laravel-mongodb/pull/2738)
* Drop support for Composer 1.x by @GromNaN in [#2784](https://github.com/mongodb/laravel-mongodb/pull/2784)
Expand Down
4 changes: 3 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function __construct(array $config)
// Select database
$this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config));

$this->tablePrefix = $config['prefix'] ?? '';

$this->useDefaultPostProcessor();

$this->useDefaultSchemaGrammar();
Expand Down Expand Up @@ -109,7 +111,7 @@ public function table($table, $as = null)
*/
public function getCollection($name)
{
return new Collection($this, $this->db->selectCollection($name));
return new Collection($this, $this->db->selectCollection($this->tablePrefix . $name));
}

/** @inheritdoc */
Expand Down
16 changes: 16 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public function testConnectionConfig(string $expectedUri, string $expectedDataba

$this->assertSame($expectedUri, (string) $client);
$this->assertSame($expectedDatabaseName, $connection->getMongoDB()->getDatabaseName());
$this->assertSame('foo', $connection->getCollection('foo')->getCollectionName());
$this->assertSame('foo', $connection->collection('foo')->raw()->getCollectionName());
}

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

public function testPrefix()
{
$config = [
'dsn' => 'mongodb://127.0.0.1/',
'database' => 'tests',
'prefix' => 'prefix_',
];

$connection = new Connection($config);

$this->assertSame('prefix_foo', $connection->getCollection('foo')->getCollectionName());
$this->assertSame('prefix_foo', $connection->collection('foo')->raw()->getCollectionName());
}

public function testQueryLog()
{
DB::enableQueryLog();
Expand Down
Loading