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\WriteConcern; |
27
|
|
|
|
28
|
|
|
const PROFILING_OFF = 0; |
29
|
|
|
const PROFILING_SLOW = 1; |
30
|
|
|
const PROFILING_ON = 2; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \MongoDB\Database |
34
|
|
|
*/ |
35
|
|
|
protected $db; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates a new database |
39
|
|
|
* |
40
|
|
|
* 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()}. |
41
|
|
|
* @link http://www.php.net/manual/en/mongodb.construct.php |
42
|
|
|
* @param MongoClient $conn Database connection. |
43
|
|
|
* @param string $name Database name. |
44
|
|
|
* @throws Exception |
45
|
|
|
* @return MongoDB Returns the database. |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function __construct($conn, $name) |
48
|
|
|
{ |
49
|
|
|
$this->connection = $conn; |
|
|
|
|
50
|
|
|
$this->name = $name; |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->setReadPreferenceFromArray($conn->getReadPreference()); |
53
|
|
|
$this->setWriteConcernFromArray($conn->getWriteConcern()); |
54
|
|
|
|
55
|
|
|
$this->createDatabaseObject(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return \MongoDB\Database |
60
|
|
|
* @internal |
61
|
|
|
*/ |
62
|
|
|
public function getDb() |
63
|
|
|
{ |
64
|
|
|
return $this->db; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The name of this database |
69
|
|
|
* @link http://www.php.net/manual/en/mongodb.--tostring.php |
70
|
|
|
* @return string Returns this database's name. |
71
|
|
|
*/ |
72
|
|
|
public function __toString() |
73
|
|
|
{ |
74
|
|
|
return $this->name; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets a collection |
79
|
|
|
* |
80
|
|
|
* @link http://www.php.net/manual/en/mongodb.get.php |
81
|
|
|
* @param string $name The name of the collection. |
82
|
|
|
* @return MongoCollection |
83
|
|
|
*/ |
84
|
|
|
public function __get($name) |
85
|
|
|
{ |
86
|
|
|
// Handle w and wtimeout properties that replicate data stored in $readPreference |
87
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
88
|
|
|
return $this->getWriteConcern()[$name]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->selectCollection($name); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name |
96
|
|
|
* @param mixed $value |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function __set($name, $value) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
101
|
|
|
$this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern()); |
102
|
|
|
$this->createDatabaseObject(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* (PECL mongo >= 1.3.0)<br/> |
108
|
|
|
* @link http://www.php.net/manual/en/mongodb.getcollectionnames.php |
109
|
|
|
* Get all collections from this database |
110
|
|
|
* @return array Returns the names of the all the collections in the database as an |
111
|
|
|
* {@link http://www.php.net/manual/en/language.types.array.php array}. |
112
|
|
|
*/ |
113
|
|
|
public function getCollectionNames(array $options = []) |
114
|
|
|
{ |
115
|
|
|
if (is_bool($options)) { |
116
|
|
|
$options = ['includeSystemCollections' => $options]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$collections = $this->db->listCollections($options); |
120
|
|
|
|
121
|
|
|
$getCollectionName = function (CollectionInfo $collectionInfo) { |
122
|
|
|
return $collectionInfo->getName(); |
123
|
|
|
}; |
124
|
|
|
|
125
|
|
|
return array_map($getCollectionName, (array) $collections); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return MongoClient |
130
|
|
|
* @internal This method is not part of the ext-mongo API |
131
|
|
|
*/ |
132
|
|
|
public function getConnection() |
133
|
|
|
{ |
134
|
|
|
return $this->connection; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
139
|
|
|
* Fetches toolkit for dealing with files stored in this database |
140
|
|
|
* @link http://www.php.net/manual/en/mongodb.getgridfs.php |
141
|
|
|
* @param string $prefix [optional] The prefix for the files and chunks collections. |
142
|
|
|
* @return MongoGridFS Returns a new gridfs object for this database. |
143
|
|
|
*/ |
144
|
|
|
public function getGridFS($prefix = "fs") |
145
|
|
|
{ |
146
|
|
|
return new \MongoGridFS($this, $prefix, $prefix); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
151
|
|
|
* Gets this database's profiling level |
152
|
|
|
* @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php |
153
|
|
|
* @return int Returns the profiling level. |
154
|
|
|
*/ |
155
|
|
|
public function getProfilingLevel() |
156
|
|
|
{ |
157
|
|
|
return static::PROFILING_OFF; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* (PECL mongo >= 1.1.0)<br/> |
162
|
|
|
* Get slaveOkay setting for this database |
163
|
|
|
* @link http://www.php.net/manual/en/mongodb.getslaveokay.php |
164
|
|
|
* @return bool Returns the value of slaveOkay for this instance. |
165
|
|
|
*/ |
166
|
|
|
public function getSlaveOkay() |
167
|
|
|
{ |
168
|
|
|
return false; |
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
|
|
|
* (PECL mongo >= 1.1.0)<br/> |
223
|
|
|
* Change slaveOkay setting for this database |
224
|
|
|
* @link http://php.net/manual/en/mongodb.setslaveokay.php |
225
|
|
|
* @param bool $ok [optional] <p> |
226
|
|
|
* If reads should be sent to secondary members of a replica set for all |
227
|
|
|
* possible queries using this {@link http://www.php.net/manual/en/class.mongodb.php MongoDB} instance. |
228
|
|
|
* </p> |
229
|
|
|
* @return bool Returns the former value of slaveOkay for this instance. |
230
|
|
|
*/ |
231
|
|
|
public function setSlaveOkay ($ok = true) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Creates a collection |
238
|
|
|
* @link http://www.php.net/manual/en/mongodb.createcollection.php |
239
|
|
|
* @param string $name The name of the collection. |
240
|
|
|
* @param array $options [optional] <p> |
241
|
|
|
* <p> |
242
|
|
|
* An array containing options for the collections. Each option is its own |
243
|
|
|
* element in the options array, with the option name listed below being |
244
|
|
|
* the key of the element. The supported options depend on the MongoDB |
245
|
|
|
* server version. At the moment, the following options are supported: |
246
|
|
|
* </p> |
247
|
|
|
* <p> |
248
|
|
|
* <b>capped</b> |
249
|
|
|
* <p> |
250
|
|
|
* If the collection should be a fixed size. |
251
|
|
|
* </p> |
252
|
|
|
* </p> |
253
|
|
|
* <p> |
254
|
|
|
* <b>size</b> |
255
|
|
|
* <p> |
256
|
|
|
* If the collection is fixed size, its size in bytes.</p></p> |
257
|
|
|
* <p><b>max</b> |
258
|
|
|
* <p>If the collection is fixed size, the maximum number of elements to store in the collection.</p></p> |
259
|
|
|
* <i>autoIndexId</i> |
260
|
|
|
* |
261
|
|
|
* <p> |
262
|
|
|
* If capped is <b>TRUE</b> you can specify <b>FALSE</b> to disable the |
263
|
|
|
* automatic index created on the <em>_id</em> field. |
264
|
|
|
* Before MongoDB 2.2, the default value for |
265
|
|
|
* <em>autoIndexId</em> was <b>FALSE</b>. |
266
|
|
|
* </p> |
267
|
|
|
* </p> |
268
|
|
|
* @return MongoCollection <p>Returns a collection object representing the new collection.</p> |
269
|
|
|
*/ |
270
|
|
|
public function createCollection($name, $options) |
271
|
|
|
{ |
272
|
|
|
$this->db->createCollection($name, $options); |
273
|
|
|
return $this->selectCollection($name); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
278
|
|
|
* @deprecated Use MongoCollection::drop() instead. |
279
|
|
|
* Drops a collection |
280
|
|
|
* @link http://www.php.net/manual/en/mongodb.dropcollection.php |
281
|
|
|
* @param MongoCollection|string $coll MongoCollection or name of collection to drop. |
282
|
|
|
* @return array Returns the database response. |
283
|
|
|
*/ |
284
|
|
|
public function dropCollection($coll) |
285
|
|
|
{ |
286
|
|
|
return $this->db->dropCollection((string) $coll); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
291
|
|
|
* Get a list of collections in this database |
292
|
|
|
* @link http://www.php.net/manual/en/mongodb.listcollections.php |
293
|
|
|
* @param bool $includeSystemCollections [optional] <p>Include system collections.</p> |
|
|
|
|
294
|
|
|
* @return array Returns a list of MongoCollections. |
295
|
|
|
*/ |
296
|
|
|
public function listCollections(array $options = []) |
297
|
|
|
{ |
298
|
|
|
return array_map([$this, 'selectCollection'], $this->getCollectionNames($options)); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
303
|
|
|
* Creates a database reference |
304
|
|
|
* @link http://www.php.net/manual/en/mongodb.createdbref.php |
305
|
|
|
* @param string $collection The collection to which the database reference will point. |
306
|
|
|
* @param mixed $document_or_id <p> |
307
|
|
|
* If an array or object is given, its <em>_id</em> field will be |
308
|
|
|
* used as the reference ID. If a {@see MongoId} or scalar |
309
|
|
|
* is given, it will be used as the reference ID. |
310
|
|
|
* </p> |
311
|
|
|
* @return array <p>Returns a database reference array.</p> |
312
|
|
|
* <p> |
313
|
|
|
* If an array without an <em>_id</em> field was provided as the |
314
|
|
|
* <em>document_or_id</em> parameter, <b>NULL</b> will be returned. |
315
|
|
|
* </p> |
316
|
|
|
*/ |
317
|
|
|
public function createDBRef($collection, $document_or_id) |
318
|
|
|
{ |
319
|
|
|
if (is_object($document_or_id)) { |
320
|
|
|
$id = isset($document_or_id->_id) ? $document_or_id->_id : null; |
321
|
|
|
// $id = $document_or_id->_id ?? null; |
|
|
|
|
322
|
|
|
} elseif (is_array($document_or_id)) { |
323
|
|
|
if (! isset($document_or_id['_id'])) { |
324
|
|
|
return null; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$id = $document_or_id['_id']; |
328
|
|
|
} else { |
329
|
|
|
$id = $document_or_id; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return [ |
333
|
|
|
'$ref' => $collection, |
334
|
|
|
'$id' => $id, |
335
|
|
|
'$db' => $this->name, |
|
|
|
|
336
|
|
|
]; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* (PECL mongo >= 0.9.0)<br/> |
342
|
|
|
* Fetches the document pointed to by a database reference |
343
|
|
|
* @link http://www.php.net/manual/en/mongodb.getdbref.php |
344
|
|
|
* @param array $ref A database reference. |
345
|
|
|
* @return array Returns the document pointed to by the reference. |
346
|
|
|
*/ |
347
|
|
|
public function getDBRef(array $ref) |
|
|
|
|
348
|
|
|
{ |
349
|
|
|
$this->notImplemented(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* (PECL mongo >= 0.9.3)<br/> |
354
|
|
|
* Runs JavaScript code on the database server. |
355
|
|
|
* @link http://www.php.net/manual/en/mongodb.execute.php |
356
|
|
|
* @param MongoCode|string $code Code to execute. |
357
|
|
|
* @param array $args [optional] Arguments to be passed to code. |
358
|
|
|
* @return array Returns the result of the evaluation. |
359
|
|
|
*/ |
360
|
|
|
public function execute($code, array $args = array()) |
|
|
|
|
361
|
|
|
{ |
362
|
|
|
$this->notImplemented(); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Execute a database command |
367
|
|
|
* @link http://www.php.net/manual/en/mongodb.command.php |
368
|
|
|
* @param array $data The query to send. |
369
|
|
|
* @param array() $options [optional] <p> |
|
|
|
|
370
|
|
|
* This parameter is an associative array of the form |
371
|
|
|
* <em>array("optionname" => <boolean>, ...)</em>. Currently |
372
|
|
|
* supported options are: |
373
|
|
|
* </p><ul> |
374
|
|
|
* <li><p><em>"timeout"</em></p><p>Deprecated alias for <em>"socketTimeoutMS"</em>.</p></li> |
375
|
|
|
* </ul> |
376
|
|
|
* @return array Returns database response. |
377
|
|
|
* Every database response is always maximum one document, |
378
|
|
|
* which means that the result of a database command can never exceed 16MB. |
379
|
|
|
* The resulting document's structure depends on the command, |
380
|
|
|
* but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents. |
381
|
|
|
*/ |
382
|
|
|
public function command(array $data, $options, &$hash) |
|
|
|
|
383
|
|
|
{ |
384
|
|
|
try { |
385
|
|
|
$cursor = new \MongoCommandCursor($this->connection, $this->name, $data); |
|
|
|
|
386
|
|
|
$cursor->setReadPreference($this->getReadPreference()); |
|
|
|
|
387
|
|
|
|
388
|
|
|
return iterator_to_array($cursor)[0]; |
389
|
|
|
} catch (\MongoDB\Driver\Exception\RuntimeException $e) { |
|
|
|
|
390
|
|
|
return [ |
391
|
|
|
'ok' => 0, |
392
|
|
|
'errmsg' => $e->getMessage(), |
393
|
|
|
'code' => $e->getCode(), |
394
|
|
|
]; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
400
|
|
|
* Check if there was an error on the most recent db operation performed |
401
|
|
|
* @link http://www.php.net/manual/en/mongodb.lasterror.php |
402
|
|
|
* @return array Returns the error, if there was one. |
403
|
|
|
*/ |
404
|
|
|
public function lastError() |
405
|
|
|
{ |
406
|
|
|
$this->notImplemented(); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
411
|
|
|
* Checks for the last error thrown during a database operation |
412
|
|
|
* @link http://www.php.net/manual/en/mongodb.preverror.php |
413
|
|
|
* @return array Returns the error and the number of operations ago it occurred. |
414
|
|
|
*/ |
415
|
|
|
public function prevError() |
416
|
|
|
{ |
417
|
|
|
$this->notImplemented(); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
422
|
|
|
* Clears any flagged errors on the database |
423
|
|
|
* @link http://www.php.net/manual/en/mongodb.reseterror.php |
424
|
|
|
* @return array Returns the database response. |
425
|
|
|
*/ |
426
|
|
|
public function resetError() |
427
|
|
|
{ |
428
|
|
|
$this->notImplemented(); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* (PECL mongo >= 0.9.5)<br/> |
433
|
|
|
* Creates a database error |
434
|
|
|
* @link http://www.php.net/manual/en/mongodb.forceerror.php |
435
|
|
|
* @return boolean Returns the database response. |
436
|
|
|
*/ |
437
|
|
|
public function forceError() |
438
|
|
|
{ |
439
|
|
|
$this->notImplemented(); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* (PECL mongo >= 1.0.1)<br/> |
444
|
|
|
* Log in to this database |
445
|
|
|
* @link http://www.php.net/manual/en/mongodb.authenticate.php |
446
|
|
|
* @param string $username The username. |
447
|
|
|
* @param string $password The password (in plaintext). |
448
|
|
|
* @return array <p>Returns database response. If the login was successful, it will return 1.</p> |
449
|
|
|
* <p> |
450
|
|
|
* <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> |
451
|
|
|
* </span> |
452
|
|
|
* </code></div> |
453
|
|
|
* </div> |
454
|
|
|
* </p> |
455
|
|
|
* <p> If something went wrong, it will return </p> |
456
|
|
|
* <p> |
457
|
|
|
* <div class="example-contents"> |
458
|
|
|
* <div class="phpcode"><code><span style="color: #000000"> |
459
|
|
|
* <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> |
460
|
|
|
* <p>("auth fails" could be another message, depending on database version and |
461
|
|
|
* what went wrong)</p> |
462
|
|
|
*/ |
463
|
|
|
public function authenticate($username, $password) |
|
|
|
|
464
|
|
|
{ |
465
|
|
|
$this->notImplemented(); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* {@inheritdoc} |
470
|
|
|
*/ |
471
|
|
|
public function setReadPreference($readPreference, $tags = null) |
472
|
|
|
{ |
473
|
|
|
$result = $this->setReadPreferenceFromParameters($readPreference, $tags); |
474
|
|
|
$this->createDatabaseObject(); |
475
|
|
|
|
476
|
|
|
return $result; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* {@inheritdoc} |
481
|
|
|
*/ |
482
|
|
|
public function setWriteConcern($wstring, $wtimeout = 0) |
483
|
|
|
{ |
484
|
|
|
$result = $this->setWriteConcernFromParameters($wstring, $wtimeout); |
485
|
|
|
$this->createDatabaseObject(); |
486
|
|
|
|
487
|
|
|
return $result; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
protected function notImplemented() |
491
|
|
|
{ |
492
|
|
|
throw new \Exception('Not implemented'); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* @return \MongoDB\Database |
497
|
|
|
*/ |
498
|
|
View Code Duplication |
private function createDatabaseObject() |
|
|
|
|
499
|
|
|
{ |
500
|
|
|
$options = [ |
501
|
|
|
'readPreference' => $this->readPreference, |
502
|
|
|
'writeConcern' => $this->writeConcern, |
503
|
|
|
]; |
504
|
|
|
|
505
|
|
|
if ($this->db === null) { |
506
|
|
|
$this->db = $this->connection->getClient()->selectDatabase($this->name, $options); |
|
|
|
|
507
|
|
|
} else { |
508
|
|
|
$this->db = $this->db->withOptions($options); |
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
|
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.