note 110765 added to class.mongoid

From: Date: Wed, 05 Dec 2012 17:46:55 +0000
Subject: note 110765 added to class.mongoid
Groups: php.notes 
Request: Send a blank email to php-notes+get-192275@lists.php.net to get a copy of this message
it is important to note that <?php array("_id" => new MongoId("50cf7d2841d41f4f35000000")) // ≠ array("_id" => array("$id" => "50cf7d2841d41f4f35000000")) ?> This issue can arrise when using json_encode() and json_decode(). If not paying close enough attention one can assume due to the encoded value of the object that it is just this simple: <?php $item = $db->myCollection->findOne(); print json_encode($item); // {"_id": {"$id": "50cf7d2841d41f4f35000000"}} $item = $db->myCollection->findOne(json_encode($item)); // $item is empty aka not found ?> Simple solution to handle these situations: <?php class MongoId2 extends MongoId { public function __construct($id = null) { if(is_array($id)) { $id = (object) $id; } if(is_object($id) && isset($id->{'$id'})) { $id = $id->{'$id'}; } return parent::__construct($id); } } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 72.52.91.2 (proxied: 67.78.233.242) ---- Manual Page -- http://php.net/manual/en/class.mongoid.php Edit -- https://master.php.net/note/edit/110765 Del: integrated -- https://master.php.net/note/delete/110765/integrated Del: useless -- https://master.php.net/note/delete/110765/useless Del: bad code -- https://master.php.net/note/delete/110765/bad+code Del: spam -- https://master.php.net/note/delete/110765/spam Del: non-english -- https://master.php.net/note/delete/110765/non-english Del: in docs -- https://master.php.net/note/delete/110765/in+docs Del: other reasons-- https://master.php.net/note/delete/110765 Reject -- https://master.php.net/note/reject/110765 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#192275) next »