1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Alcaeus\MongoDbAdapter\Helper; |
17
|
|
|
use MongoDB\Model\CollectionInfo; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instances of this class are used to interact with a database. |
21
|
|
|
* @link http://www.php.net/manual/en/class.mongodb.php |
22
|
|
|
*/ |
23
|
|
|
class MongoDB |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
use Helper\ReadPreference; |
26
|
|
|
use Helper\SlaveOkay; |
27
|
|
|
use Helper\WriteConcern; |
28
|
|
|
|
29
|
|
|
const PROFILING_OFF = 0; |
30
|
|
|
const PROFILING_SLOW = 1; |
31
|
|
|
const PROFILING_ON = 2; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var MongoClient |
35
|
|
|
*/ |
36
|
|
|
protected $connection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \MongoDB\Database |
40
|
|
|
*/ |
41
|
|
|
protected $db; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $name; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates a new database |
50
|
|
|
* |
51
|
|
|
* This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through {@see Mongo::__get()} or {@see Mongo::selectDB()}. |
52
|
|
|
* @link http://www.php.net/manual/en/mongodb.construct.php |
53
|
|
|
* @param MongoClient $conn Database connection. |
54
|
|
|
* @param string $name Database name. |
55
|
|
|
* @throws Exception |
56
|
|
|
* @return MongoDB Returns the database. |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function __construct(MongoClient $conn, $name) |
59
|
|
|
{ |
60
|
|
|
$this->connection = $conn; |
61
|
|
|
$this->name = $name; |
62
|
|
|
|
63
|
|
|
$this->setReadPreferenceFromArray($conn->getReadPreference()); |
64
|
|
|
$this->setWriteConcernFromArray($conn->getWriteConcern()); |
65
|
|
|
|
66
|
|
|
$this->createDatabaseObject(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \MongoDB\Database |
71
|
|
|
* @internal |
72
|
|
|
*/ |
73
|
|
|
public function getDb() |
74
|
|
|
{ |
75
|
|
|
return $this->db; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The name of this database |
80
|
|
|
* @link http://www.php.net/manual/en/mongodb.--tostring.php |
81
|
|
|
* @return string Returns this database's name. |
82
|
|
|
*/ |
83
|
|
|
public function __toString() |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets a collection |
90
|
|
|
* |
91
|
|
|
* @link http://www.php.net/manual/en/mongodb.get.php |
92
|
|
|
* @param string $name The name of the collection. |
93
|
|
|
* @return MongoCollection |
94
|
|
|
*/ |
95
|
|
|
public function __get($name) |
96
|
|
|
{ |
97
|
|
|
// Handle w and wtimeout properties that replicate data stored in $readPreference |
98
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
99
|
|
|
return $this->getWriteConcern()[$name]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->selectCollection($name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $name |
107
|
|
|
* @param mixed $value |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function __set($name, $value) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
112
|
|
|
$this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern()); |
113
|
|
|
$this->createDatabaseObject(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* (PECL mongo >= 1.3.0)<br/> |
119
|
|
|
* @link http://www.php.net/manual/en/mongodb.getcollectionnames.php |
120
|
|
|
* Get all collections from this database |
121
|
|
|
* @return array Returns the names of the all the collections in the database as an |
122
|
|
|
* {@link http://www.php.net/manual/en/language.types.array.php array}. |
123
|
|
|
*/ |
124
|
|
|
public function getCollectionNames(array $options = []) |
125
|
|
|
{ |
126
|
|
|
if (is_bool($options)) { |
127
|
|
|
$options = ['includeSystemCollections' => $options]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$collections = $this->db->listCollections($options); |
131
|
|
|
|
132
|
|
|
$getCollectionName = function (CollectionInfo $collectionInfo) { |
133
|
|
|
return $collectionInfo->getName(); |
134
|
|
|
}; |
135
|
|
|
|
136
|
|
|
return array_map($getCollectionName, (array) $collections); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return MongoClient |
141
|
|
|
* @internal This method is not part of the ext-mongo API |
142
|
|
|
*/ |
143
|
|
|
public function getConnection() |
144
|
|
|
{ |
145
|
|
|
return $this->connection; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
150
|
|
|
* Fetches toolkit for dealing with files stored in this database |
151
|
|
|
* @link http://www.php.net/manual/en/mongodb.getgridfs.php |
152
|
|
|
* @param string $prefix [optional] The prefix for the files and chunks collections. |
153
|
|
|
* @return MongoGridFS Returns a new gridfs object for this database. |
154
|
|
|
*/ |
155
|
|
|
public function getGridFS($prefix = "fs") |
156
|
|
|
{ |
157
|
|
|
return new \MongoGridFS($this, $prefix, $prefix); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
162
|
|
|
* Gets this database's profiling level |
163
|
|
|
* @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php |
164
|
|
|
* @return int Returns the profiling level. |
165
|
|
|
*/ |
166
|
|
|
public function getProfilingLevel() |
167
|
|
|
{ |
168
|
|
|
return static::PROFILING_OFF; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
173
|
|
|
* Sets this database's profiling level |
174
|
|
|
* @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php |
175
|
|
|
* @param int $level Profiling level. |
176
|
|
|
* @return int Returns the previous profiling level. |
177
|
|
|
*/ |
178
|
|
|
public function setProfilingLevel($level) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
return static::PROFILING_OFF; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
185
|
|
|
* Drops this database |
186
|
|
|
* @link http://www.php.net/manual/en/mongodb.drop.php |
187
|
|
|
* @return array Returns the database response. |
188
|
|
|
*/ |
189
|
|
|
public function drop() |
190
|
|
|
{ |
191
|
|
|
return $this->db->drop(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Repairs and compacts this database |
196
|
|
|
* @link http://www.php.net/manual/en/mongodb.repair.php |
197
|
|
|
* @param bool $preserve_cloned_files [optional] <p>If cloned files should be kept if the repair fails.</p> |
198
|
|
|
* @param bool $backup_original_files [optional] <p>If original files should be backed up.</p> |
199
|
|
|
* @return array <p>Returns db response.</p> |
200
|
|
|
*/ |
201
|
|
|
public function repair($preserve_cloned_files = FALSE, $backup_original_files = FALSE) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
return []; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
208
|
|
|
* Gets a collection |
209
|
|
|
* @link http://www.php.net/manual/en/mongodb.selectcollection.php |
210
|
|
|
* @param string $name <b>The collection name.</b> |
211
|
|
|
* @throws Exception if the collection name is invalid. |
212
|
|
|
* @return MongoCollection <p> |
213
|
|
|
* Returns a new collection object. |
214
|
|
|
* </p> |
215
|
|
|
*/ |
216
|
|
|
public function selectCollection($name) |
217
|
|
|
{ |
218
|
|
|
return new MongoCollection($this, $name); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Creates a collection |
223
|
|
|
* @link http://www.php.net/manual/en/mongodb.createcollection.php |
224
|
|
|
* @param string $name The name of the collection. |
225
|
|
|
* @param array $options [optional] <p> |
226
|
|
|
* <p> |
227
|
|
|
* An array containing options for the collections. Each option is its own |
228
|
|
|
* element in the options array, with the option name listed below being |
229
|
|
|
* the key of the element. The supported options depend on the MongoDB |
230
|
|
|
* server version. At the moment, the following options are supported: |
231
|
|
|
* </p> |
232
|
|
|
* <p> |
233
|
|
|
* <b>capped</b> |
234
|
|
|
* <p> |
235
|
|
|
* If the collection should be a fixed size. |
236
|
|
|
* </p> |
237
|
|
|
* </p> |
238
|
|
|
* <p> |
239
|
|
|
* <b>size</b> |
240
|
|
|
* <p> |
241
|
|
|
* If the collection is fixed size, its size in bytes.</p></p> |
242
|
|
|
* <p><b>max</b> |
243
|
|
|
* <p>If the collection is fixed size, the maximum number of elements to store in the collection.</p></p> |
244
|
|
|
* <i>autoIndexId</i> |
245
|
|
|
* |
246
|
|
|
* <p> |
247
|
|
|
* If capped is <b>TRUE</b> you can specify <b>FALSE</b> to disable the |
248
|
|
|
* automatic index created on the <em>_id</em> field. |
249
|
|
|
* Before MongoDB 2.2, the default value for |
250
|
|
|
* <em>autoIndexId</em> was <b>FALSE</b>. |
251
|
|
|
* </p> |
252
|
|
|
* </p> |
253
|
|
|
* @return MongoCollection <p>Returns a collection object representing the new collection.</p> |
254
|
|
|
*/ |
255
|
|
|
public function createCollection($name, $options) |
256
|
|
|
{ |
257
|
|
|
$this->db->createCollection($name, $options); |
258
|
|
|
return $this->selectCollection($name); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
263
|
|
|
* @deprecated Use MongoCollection::drop() instead. |
264
|
|
|
* Drops a collection |
265
|
|
|
* @link http://www.php.net/manual/en/mongodb.dropcollection.php |
266
|
|
|
* @param MongoCollection|string $coll MongoCollection or name of collection to drop. |
267
|
|
|
* @return array Returns the database response. |
268
|
|
|
*/ |
269
|
|
|
public function dropCollection($coll) |
270
|
|
|
{ |
271
|
|
|
return $this->db->dropCollection((string) $coll); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
276
|
|
|
* Get a list of collections in this database |
277
|
|
|
* @link http://www.php.net/manual/en/mongodb.listcollections.php |
278
|
|
|
* @param bool $includeSystemCollections [optional] <p>Include system collections.</p> |
|
|
|
|
279
|
|
|
* @return array Returns a list of MongoCollections. |
280
|
|
|
*/ |
281
|
|
|
public function listCollections(array $options = []) |
282
|
|
|
{ |
283
|
|
|
return array_map([$this, 'selectCollection'], $this->getCollectionNames($options)); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
288
|
|
|
* Creates a database reference |
289
|
|
|
* @link http://www.php.net/manual/en/mongodb.createdbref.php |
290
|
|
|
* @param string $collection The collection to which the database reference will point. |
291
|
|
|
* @param mixed $document_or_id <p> |
292
|
|
|
* If an array or object is given, its <em>_id</em> field will be |
293
|
|
|
* used as the reference ID. If a {@see MongoId} or scalar |
294
|
|
|
* is given, it will be used as the reference ID. |
295
|
|
|
* </p> |
296
|
|
|
* @return array <p>Returns a database reference array.</p> |
297
|
|
|
* <p> |
298
|
|
|
* If an array without an <em>_id</em> field was provided as the |
299
|
|
|
* <em>document_or_id</em> parameter, <b>NULL</b> will be returned. |
300
|
|
|
* </p> |
301
|
|
|
*/ |
302
|
|
|
public function createDBRef($collection, $document_or_id) |
303
|
|
|
{ |
304
|
|
|
if (is_object($document_or_id)) { |
305
|
|
|
$id = isset($document_or_id->_id) ? $document_or_id->_id : null; |
306
|
|
|
// $id = $document_or_id->_id ?? null; |
|
|
|
|
307
|
|
|
} elseif (is_array($document_or_id)) { |
308
|
|
|
if (! isset($document_or_id['_id'])) { |
309
|
|
|
return null; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$id = $document_or_id['_id']; |
313
|
|
|
} else { |
314
|
|
|
$id = $document_or_id; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return [ |
318
|
|
|
'$ref' => $collection, |
319
|
|
|
'$id' => $id, |
320
|
|
|
'$db' => $this->name, |
321
|
|
|
]; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
327
|
|
|
* Fetches the document pointed to by a database reference |
328
|
|
|
* @link http://www.php.net/manual/en/mongodb.getdbref.php |
329
|
|
|
* @param array $ref A database reference. |
330
|
|
|
* @return array Returns the document pointed to by the reference. |
331
|
|
|
*/ |
332
|
|
|
public function getDBRef(array $ref) |
|
|
|
|
333
|
|
|
{ |
334
|
|
|
$this->notImplemented(); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* (PECL mongo >= 0.9.3)<br/> |
339
|
|
|
* Runs JavaScript code on the database server. |
340
|
|
|
* @link http://www.php.net/manual/en/mongodb.execute.php |
341
|
|
|
* @param MongoCode|string $code Code to execute. |
342
|
|
|
* @param array $args [optional] Arguments to be passed to code. |
343
|
|
|
* @return array Returns the result of the evaluation. |
344
|
|
|
*/ |
345
|
|
|
public function execute($code, array $args = array()) |
|
|
|
|
346
|
|
|
{ |
347
|
|
|
$this->notImplemented(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Execute a database command |
352
|
|
|
* @link http://www.php.net/manual/en/mongodb.command.php |
353
|
|
|
* @param array $data The query to send. |
354
|
|
|
* @param array() $options [optional] <p> |
|
|
|
|
355
|
|
|
* This parameter is an associative array of the form |
356
|
|
|
* <em>array("optionname" => <boolean>, ...)</em>. Currently |
357
|
|
|
* supported options are: |
358
|
|
|
* </p><ul> |
359
|
|
|
* <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li> |
360
|
|
|
* </ul> |
361
|
|
|
* @return array Returns database response. |
362
|
|
|
* Every database response is always maximum one document, |
363
|
|
|
* which means that the result of a database command can never exceed 16MB. |
364
|
|
|
* The resulting document's structure depends on the command, |
365
|
|
|
* but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents. |
366
|
|
|
*/ |
367
|
|
|
public function command(array $data, $options, &$hash) |
|
|
|
|
368
|
|
|
{ |
369
|
|
|
try { |
370
|
|
|
$cursor = new \MongoCommandCursor($this->connection, $this->name, $data); |
371
|
|
|
$cursor->setReadPreference($this->getReadPreference()); |
|
|
|
|
372
|
|
|
|
373
|
|
|
return iterator_to_array($cursor)[0]; |
374
|
|
|
} catch (\MongoDB\Driver\Exception\RuntimeException $e) { |
|
|
|
|
375
|
|
|
return [ |
376
|
|
|
'ok' => 0, |
377
|
|
|
'errmsg' => $e->getMessage(), |
378
|
|
|
'code' => $e->getCode(), |
379
|
|
|
]; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
385
|
|
|
* Check if there was an error on the most recent db operation performed |
386
|
|
|
* @link http://www.php.net/manual/en/mongodb.lasterror.php |
387
|
|
|
* @return array Returns the error, if there was one. |
388
|
|
|
*/ |
389
|
|
|
public function lastError() |
390
|
|
|
{ |
391
|
|
|
$this->notImplemented(); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
396
|
|
|
* Checks for the last error thrown during a database operation |
397
|
|
|
* @link http://www.php.net/manual/en/mongodb.preverror.php |
398
|
|
|
* @return array Returns the error and the number of operations ago it occurred. |
399
|
|
|
*/ |
400
|
|
|
public function prevError() |
401
|
|
|
{ |
402
|
|
|
$this->notImplemented(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
407
|
|
|
* Clears any flagged errors on the database |
408
|
|
|
* @link http://www.php.net/manual/en/mongodb.reseterror.php |
409
|
|
|
* @return array Returns the database response. |
410
|
|
|
*/ |
411
|
|
|
public function resetError() |
412
|
|
|
{ |
413
|
|
|
$this->notImplemented(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
418
|
|
|
* Creates a database error |
419
|
|
|
* @link http://www.php.net/manual/en/mongodb.forceerror.php |
420
|
|
|
* @return boolean Returns the database response. |
421
|
|
|
*/ |
422
|
|
|
public function forceError() |
423
|
|
|
{ |
424
|
|
|
$this->notImplemented(); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* (PECL mongo >= 1.0.1)<br/> |
429
|
|
|
* Log in to this database |
430
|
|
|
* @link http://www.php.net/manual/en/mongodb.authenticate.php |
431
|
|
|
* @param string $username The username. |
432
|
|
|
* @param string $password The password (in plaintext). |
433
|
|
|
* @return array <p>Returns database response. If the login was successful, it will return 1.</p> |
434
|
|
|
* <p> |
435
|
|
|
* <span style="color: #0000BB"><?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?></span> |
436
|
|
|
* </span> |
437
|
|
|
* </code></div> |
438
|
|
|
* </div> |
439
|
|
|
* </p> |
440
|
|
|
* <p> If something went wrong, it will return </p> |
441
|
|
|
* <p> |
442
|
|
|
* <div class="example-contents"> |
443
|
|
|
* <div class="phpcode"><code><span style="color: #000000"> |
444
|
|
|
* <span style="color: #0000BB"><?php<br></span><span style="color: #007700">array(</span><span style="color: #DD0000">"ok" </span><span style="color: #007700">=> </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">"errmsg" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"auth fails"</span><span style="color: #007700">);<br></span><span style="color: #0000BB">?></span></p> |
445
|
|
|
* <p>("auth fails" could be another message, depending on database version and |
446
|
|
|
* what went wrong)</p> |
447
|
|
|
*/ |
448
|
|
|
public function authenticate($username, $password) |
|
|
|
|
449
|
|
|
{ |
450
|
|
|
$this->notImplemented(); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* {@inheritdoc} |
455
|
|
|
*/ |
456
|
|
|
public function setReadPreference($readPreference, $tags = null) |
457
|
|
|
{ |
458
|
|
|
$result = $this->setReadPreferenceFromParameters($readPreference, $tags); |
459
|
|
|
$this->createDatabaseObject(); |
460
|
|
|
|
461
|
|
|
return $result; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* {@inheritdoc} |
466
|
|
|
*/ |
467
|
|
|
public function setWriteConcern($wstring, $wtimeout = 0) |
468
|
|
|
{ |
469
|
|
|
$result = $this->setWriteConcernFromParameters($wstring, $wtimeout); |
470
|
|
|
$this->createDatabaseObject(); |
471
|
|
|
|
472
|
|
|
return $result; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
protected function notImplemented() |
476
|
|
|
{ |
477
|
|
|
throw new \Exception('Not implemented'); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @return \MongoDB\Database |
482
|
|
|
*/ |
483
|
|
View Code Duplication |
private function createDatabaseObject() |
|
|
|
|
484
|
|
|
{ |
485
|
|
|
$options = [ |
486
|
|
|
'readPreference' => $this->readPreference, |
487
|
|
|
'writeConcern' => $this->writeConcern, |
488
|
|
|
]; |
489
|
|
|
|
490
|
|
|
if ($this->db === null) { |
491
|
|
|
$this->db = $this->connection->getClient()->selectDatabase($this->name, $options); |
492
|
|
|
} else { |
493
|
|
|
$this->db = $this->db->withOptions($options); |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.