@@ -55,6 +55,7 @@ mongodb:
55
55
clients:
56
56
default:
57
57
uri: '%env(MONGODB_URI)%'
58
+ default_database: #...
58
59
uri_options: #...
59
60
driver_options: #...
60
61
` ` `
@@ -96,7 +97,8 @@ class MyService
96
97
}
97
98
` ` `
98
99
99
- If you register multiple clients, you can autowire them by name + `Client` suffix :
100
+ If you register multiple clients, you can autowire them by using the client name with a `Client` suffix as parameter
101
+ name :
100
102
101
103
` ` ` php
102
104
use MongoDB\B undle\A ttribute\A utowireClient;
@@ -105,6 +107,7 @@ use MongoDB\Client;
105
107
class MyService
106
108
{
107
109
public function __construct(
110
+ // Will autowire the client with the id "second"
108
111
private Client $secondClient,
109
112
) {}
110
113
}
@@ -119,16 +122,16 @@ use MongoDB\Client;
119
122
class MyService
120
123
{
121
124
public function __construct(
122
- #[AutowireClient('second')]
125
+ #[AutowireClient('second')]
123
126
private Client $client,
124
127
) {}
125
128
}
126
129
` ` `
127
130
128
131
# # Database and Collection Usage
129
132
130
- The client service provides access to databases and collections. You can access a database by calling the `selectDatabase`
131
- method, passing the database name and potential options :
133
+ The client service provides access to databases and collections. You can access a database by calling the
134
+ `selectDatabase` method, passing the database name and potential options :
132
135
133
136
` ` ` php
134
137
use MongoDB\C lient;
@@ -146,7 +149,7 @@ class MyService
146
149
}
147
150
` ` `
148
151
149
- An alternative to this is using the `AutowireDatabase` attribute, referencing the database name :
152
+ An alternative to this is using the `#[ AutowireDatabase] ` attribute, referencing the database name:
150
153
151
154
` ` ` php
152
155
use MongoDB\B undle\A ttribute\A utowireDatabase;
@@ -161,21 +164,9 @@ class MyService
161
164
}
162
165
` ` `
163
166
164
- You can also omit the `database` option if the property name matches the database name.
165
- In the following example the database name is `myDatabase`, inferred from the property name :
166
-
167
- ` ` ` php
168
- use MongoDB\B undle\A ttribute\A utowireCollection;
169
- use MongoDB\C ollection;
170
-
171
- class MyService
172
- {
173
- public function __construct(
174
- #[AutowireCollection()]
175
- private Collection $myDatabase,
176
- ) {}
177
- }
178
- ` ` `
167
+ If you don't specify a database name in the attribute, the default database name (specified in the `default_database`
168
+ configuration option) will be used. If you did not define a default database, the database name has to be specified in
169
+ the attribute.
179
170
180
171
If you have more than one client defined, you can also reference the client :
181
172
@@ -193,7 +184,7 @@ class MyService
193
184
` ` `
194
185
195
186
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 :
187
+ For convenience, the `#[ AutowireCollection] ` attribute provides a quicker alternative:
197
188
198
189
` ` ` php
199
190
use MongoDB\B undle\A ttribute\A utowireCollection;
@@ -230,6 +221,7 @@ class MyService
230
221
` ` `
231
222
232
223
If you have more than one client defined, you can also reference the client :
224
+
233
225
` ` ` php
234
226
use MongoDB\B undle\A ttribute\A utowireCollection;
235
227
use MongoDB\C ollection;
@@ -245,3 +237,27 @@ class MyService
245
237
) {}
246
238
}
247
239
` ` `
240
+
241
+ By specifiying the `default_database` option in the configuration, you can omit the `database` option in the
242
+ `AutowireCollection` attribute :
243
+
244
+ ` ` ` diff
245
+ mongodb:
246
+ clients:
247
+ default:
248
+ uri: '%env(MONGODB_URI)%'
249
+ + default_database: 'myDatabase'
250
+ ` ` `
251
+
252
+ ` ` ` php
253
+ use MongoDB\B undle\A ttribute\A utowireCollection;
254
+ use MongoDB\C ollection;
255
+
256
+ class MyService
257
+ {
258
+ public function __construct(
259
+ #[AutowireCollection]
260
+ private Collection $myCollection,
261
+ ) {}
262
+ }
263
+ ` ` `
0 commit comments