Skip to content

Commit d1ee787

Browse files
OskarStarkalcaeus
authored andcommitted
Fix documentation
1 parent 7834fe6 commit d1ee787

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ mongodb:
5555
clients:
5656
default:
5757
uri: '%env(MONGODB_URI)%'
58+
default_database: #...
5859
uriOptions: #...
5960
driverOptions: #...
6061
```
@@ -146,7 +147,7 @@ class MyService
146147
}
147148
```
148149

149-
An alternative to this is using the `AutowireDatabase` attribute, referencing the database name:
150+
An alternative to this is using the `#[AutowireDatabase]` attribute, referencing the database name:
150151

151152
```php
152153
use MongoDB\Bundle\Attribute\AutowireDatabase;
@@ -166,13 +167,13 @@ In the following example the database name is `myDatabase`, inferred from the pr
166167

167168
```php
168169
use MongoDB\Bundle\Attribute\AutowireCollection;
169-
use MongoDB\Collection;
170+
use MongoDB\Database;
170171
171172
class MyService
172173
{
173174
public function __construct(
174-
#[AutowireCollection()]
175-
private Collection $myDatabase,
175+
#[AutowireDatabase]
176+
private Database $myDatabase,
176177
) {}
177178
}
178179
```
@@ -193,7 +194,7 @@ class MyService
193194
```
194195

195196
To inject a collection, you can either call the `selectCollection` method on a `Client` or `Database` instance.
196-
For convenience, the `AutowireCollection` attribute provides a quicker alternative:
197+
For convenience, the `#[AutowireCollection]` attribute provides a quicker alternative:
197198

198199
```php
199200
use MongoDB\Bundle\Attribute\AutowireCollection;
@@ -230,6 +231,7 @@ class MyService
230231
```
231232

232233
If you have more than one client defined, you can also reference the client:
234+
233235
```php
234236
use MongoDB\Bundle\Attribute\AutowireCollection;
235237
use MongoDB\Collection;
@@ -245,3 +247,26 @@ class MyService
245247
) {}
246248
}
247249
```
250+
251+
By specifiying the `default_database` option in the configuration, you can omit the `database` option in the attribute:
252+
253+
```diff
254+
mongodb:
255+
clients:
256+
default:
257+
uri: '%env(MONGODB_URI)%'
258+
+ default_database: 'myDtabase'
259+
```
260+
261+
```php
262+
use MongoDB\Bundle\Attribute\AutowireCollection;
263+
use MongoDB\Collection;
264+
265+
class MyService
266+
{
267+
public function __construct(
268+
#[AutowireCollection]
269+
private Collection $myCollection,
270+
) {}
271+
}
272+
```

0 commit comments

Comments
 (0)