note 118824 added to class.mongodb-driver-cursor
| From: | max-p at max-p dot me | Date: | Fri, 12 Feb 2016 01:29:34 +0000 |
| Subject: | note 118824 added to class.mongodb-driver-cursor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204922@lists.php.net to get a copy of this message | ||
As one might notice, this class does not implement a hasNext() or next() method as opposed to the
now deprecated Mongo driver.
If, for any reason, you need to pull data from the cursor procedurally or otherwise need to override
the behavior of foreach while iterating on the cursor, the SPL \IteratorIterator class can be used.
When doing so, it is important to rewind the iterator before using it, otherwise you won't get
any data back.
<?php
$cursor = $collection->find();
$it = new \IteratorIterator($cursor);
$it->rewind(); // Very important
while($doc = $it->current()) {
var_dump($doc);
$it->next();
}
?>
I used this trick to build a backward compatibility wrapper emulating the old Mongo driver in order
to upgrade an older codebase.
----
Server IP: 72.52.91.14
Probable Submitter: 70.49.215.47
----
Manual Page -- http://php.net/manual/en/class.mongodb-driver-cursor.php
Edit -- https://master.php.net/note/edit/118824
Del: integrated -- https://master.php.net/note/delete/118824/integrated
Del: useless -- https://master.php.net/note/delete/118824/useless
Del: bad code -- https://master.php.net/note/delete/118824/bad+code
Del: spam -- https://master.php.net/note/delete/118824/spam
Del: non-english -- https://master.php.net/note/delete/118824/non-english
Del: in docs -- https://master.php.net/note/delete/118824/in+docs
Del: other reasons-- https://master.php.net/note/delete/118824
Reject -- https://master.php.net/note/reject/118824
Search -- https://master.php.net/manage/user-notes.php