File tree Expand file tree Collapse file tree 2 files changed +34
-3
lines changed Expand file tree Collapse file tree 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)
4848 */
4949 public function find ($ id , $ columns = array ('* ' ))
5050 {
51- return $ this ->where ('_id ' , '= ' , new MongoID (( string ) $ id ))->first ($ columns );
51+ return $ this ->where ('_id ' , '= ' , $ this -> convertKey ( $ id ))->first ($ columns );
5252 }
5353
5454 /**
@@ -500,6 +500,22 @@ protected function performUpdate($query)
500500 return 0 ;
501501 }
502502
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+
503519 /**
504520 * Compile the where array
505521 *
@@ -522,13 +538,13 @@ protected function compileWheres()
522538 {
523539 foreach ($ where ['values ' ] as &$ value )
524540 {
525- $ value = ( $ value instanceof MongoID) ? $ value : new MongoID ($ value );
541+ $ value = $ this -> convertKey ($ value );
526542 }
527543 }
528544 // Single value
529545 else
530546 {
531- $ where ['value ' ] = ( $ where [ ' value ' ] instanceof MongoID) ? $ where [ ' value ' ] : new MongoID ($ where ['value ' ]);
547+ $ where ['value ' ] = $ this -> convertKey ($ where ['value ' ]);
532548 }
533549 }
534550
Original file line number Diff line number Diff line change @@ -228,4 +228,19 @@ public function testDistinct()
228228 $ this ->assertEquals (array ('sharp ' , 'round ' ), $ types );
229229 }
230230
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+
231246}
You can’t perform that action at this time.
0 commit comments