File tree 2 files changed +34
-3
lines changed
2 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ public function __construct(Connection $connection)
48
48
*/
49
49
public function find ($ id , $ columns = array ('* ' ))
50
50
{
51
- return $ this ->where ('_id ' , '= ' , new MongoID (( string ) $ id ))->first ($ columns );
51
+ return $ this ->where ('_id ' , '= ' , $ this -> convertKey ( $ id ))->first ($ columns );
52
52
}
53
53
54
54
/**
@@ -500,6 +500,22 @@ protected function performUpdate($query)
500
500
return 0 ;
501
501
}
502
502
503
+ /**
504
+ * Convert a key to MongoID if needed
505
+ *
506
+ * @param mixed $id
507
+ * @return mixed
508
+ */
509
+ protected function convertKey ($ id )
510
+ {
511
+ if (is_string ($ id ) && strlen ($ id ) === 24 && ctype_xdigit ($ id ))
512
+ {
513
+ return new MongoId ($ id );
514
+ }
515
+
516
+ return $ id ;
517
+ }
518
+
503
519
/**
504
520
* Compile the where array
505
521
*
@@ -522,13 +538,13 @@ protected function compileWheres()
522
538
{
523
539
foreach ($ where ['values ' ] as &$ value )
524
540
{
525
- $ value = ( $ value instanceof MongoID) ? $ value : new MongoID ($ value );
541
+ $ value = $ this -> convertKey ($ value );
526
542
}
527
543
}
528
544
// Single value
529
545
else
530
546
{
531
- $ where ['value ' ] = ( $ where [ ' value ' ] instanceof MongoID) ? $ where [ ' value ' ] : new MongoID ($ where ['value ' ]);
547
+ $ where ['value ' ] = $ this -> convertKey ($ where ['value ' ]);
532
548
}
533
549
}
534
550
Original file line number Diff line number Diff line change @@ -228,4 +228,19 @@ public function testDistinct()
228
228
$ this ->assertEquals (array ('sharp ' , 'round ' ), $ types );
229
229
}
230
230
231
+ public function testCustomId ()
232
+ {
233
+ DB ::collection ('items ' )->insert (array (
234
+ array ('_id ' => 'knife ' , 'type ' => 'sharp ' , 'amount ' => 34 ),
235
+ array ('_id ' => 'fork ' , 'type ' => 'sharp ' , 'amount ' => 20 ),
236
+ array ('_id ' => 'spoon ' , 'type ' => 'round ' , 'amount ' => 3 )
237
+ ));
238
+
239
+ $ item = DB ::collection ('items ' )->find ('knife ' );
240
+ $ this ->assertEquals ('knife ' , $ item ['_id ' ]);
241
+
242
+ $ item = DB ::collection ('items ' )->where ('_id ' , 'fork ' )->first ();
243
+ $ this ->assertEquals ('fork ' , $ item ['_id ' ]);
244
+ }
245
+
231
246
}
You can’t perform that action at this time.
0 commit comments