PHP driver

0 views
Skip to first unread message

Jon Moss

unread,
Mar 27, 2009, 6:37:08 PM3/27/09
to mongodb-user
Greetings all,

Quick question/request for the php driver for mongo or rather the
classes used with the mongo extension.

Currently the MongoCursor that is returned by a find() can be
traversed with something like:

while($aCursor->hasNext()){
print_r($aCursor->next());
}

It would be really great if it could actually implement the Iterator
interface (from SPL). That way you could iterator with:

foreach($aCursor as $item){
print_r($item);
}

It only requires implementing a few functions (although one of them is
next() which would break the current implementation).

it would also be great if key() returned the current Object Id for you
could do:

foreach($aCursor as $id => $item){
echo $id . ":\n";
print_r($item);
}

I don't know who is working on the php classes but if you want I could
take a stab at it?

Thanks,
Jon

Eliot

unread,
Mar 27, 2009, 6:48:54 PM3/27/09
to mongodb-user
You can definitely take a stab at it.
With github its very easy.
- Create an account
- Fork mongo-php-driver

if you get it working, then you can submit a pull request and we can
merge it back into our branch

Jon Moss

unread,
Mar 27, 2009, 7:37:21 PM3/27/09
to mongodb-user
Done :)

my fork is at git://github.com/jonathanmoss/mongo-php-driver.git

Take a look, and if you like it feel free to merge it back in. It
does break backward compatibility due to needing to implement next()
in a different way for the interface I'm afraid. I also removed
hasNext() as it is not needed any more if you use the cursor as an
iterator. I realise this is not ideal, so if you prefer I could add a
getIterator() function instead and implement the iterator in
MongoCursorIterator or similar?

I ran a few quick tests and all seemed ok.

Any suggestions are welcome.

Jon

Geir Magnusson Jr.

unread,
Mar 27, 2009, 7:39:26 PM3/27/09
to mongod...@googlegroups.com
cool - thx. We'll take a look.

geir

Jon Moss

unread,
Mar 27, 2009, 7:58:30 PM3/27/09
to mongodb-user
I have also implemented it as a function getIterator() within
MongoCursor which mean the the current version of MongoCursor remains
unchanged but you can still iterator with foreach using:

foreach($cursor->getIterator() $id => $obj){
//stuff
}

but I cannot push both options to my fork at once so just let me know
if you prefer this option.

Jon

Eliot Horowitz

unread,
Mar 27, 2009, 8:00:38 PM3/27/09
to mongod...@googlegroups.com, mongodb-user
I like the getIterator version better personally. A little clearer

Jon Moss

unread,
Mar 27, 2009, 8:11:47 PM3/27/09
to mongodb-user
Agreed,

Have pushed to my fork

Jon Moss

unread,
Mar 28, 2009, 4:22:47 AM3/28/09
to mongodb-user
Just pushed a much cleaner MongoCursorIterator to my fork. There was
no need to the duplicate code I had in it.

Let me know if you have any suggestions, Should also work in
conjunction with getGridFSCursor() e.g.

$cursor->getGridFSCursor()->getIterator();

Jon

Kristina Chodorow

unread,
Mar 29, 2009, 12:10:25 PM3/29/09
to mongod...@googlegroups.com
Hi!  I'm the PHP driver person.  Your change looks great, I'll merge it in on Monday.  I had originally modeled the cursor API after our JavaScript driver's, but I like your idea better.

It's okay that it breaks backwards compatibility, as the driver is still in alpha. 

Thanks for the patch!

Jon Moss

unread,
Mar 31, 2009, 2:44:26 AM3/31/09
to mongodb-user

Hi Kristina,

Your PHP stuff is awesome! I am more than happy to contribute. If you
have any suggestions/improvements, let me know.

Jon

Kristina Chodorow

unread,
Apr 1, 2009, 10:56:56 AM4/1/09
to mongod...@googlegroups.com
Thank you! 

I finally merged in your changes.  I decided to put them in MongoCursor, instead of having a separate MongoCursorIterator class.

You can see some examples using the iterator at http://github.com/mongodb/mongo-php-driver/blob/master/tests/cursor_iterator.phpt.

Jon Moss

unread,
Apr 1, 2009, 9:15:56 PM4/1/09
to mongodb-user
Excellent,

I also think merging it with Cursor.php is the most convenient option.

Thanks,
Jon
Reply all
Reply to author
Forward
0 new messages