simplemongophp -- a simple library for Mongo PHP

Showing 1-2 of 2 messages
simplemongophp -- a simple library for Mongo PHP Ian White 7/27/09 10:52 PM
Hello Mongolians,

For the past few months in the development of The Business Insider (http://www.businessinsider.com), we've been using this library. We recently switched to using PHP (we've been using Mongo itself for some time now), so this code is currently in use in production. Today I finally got around to PHPDocumenting it and I figured I'd make it available in case anyone else finds it useful.

It's really nothing fancy -- just an easy way of using data objects without a lot of overhead or abstraction. If you have no interest in data objects, you still might find the Db class useful to smooth out some of the rough edges of the PHP API (at a minimum, it'll cut down on your selectCollection() calls!)

Github URL: http://github.com/ibwhite/simplemongophp/tree/master

Sample usage:

   require('Db.php');
   require('Dbo.php');

   $mongo = new Mongo();
   define('MONGODB_NAME', 'lost');
   class LostPerson extends Dbo {
     function rollCall() {
       echo "$this->name is a " . ($this->goodguy ? 'good' : 'bad') . " guy!\n";
     }
   }
   Dbo::addClass('LostPerson', 'people');

   Db::drop('people');
   Db::batchInsert('people', array(
     array('name' => 'Jack', 'sex' => 'M', 'goodguy' => true),
     array('name' => 'Kate', 'sex' => 'F', 'goodguy' => true),
     array('name' => 'Locke', 'sex' => 'M', 'goodguy' => true),
     array('name' => 'Hurley', 'sex' => 'M', 'goodguy' => true),
     array('name' => 'Ben', 'sex' => 'M', 'goodguy' => false),
   ));
   foreach (Dbo::find('LostPerson', array('goodguy' => true), array('sort' => array('name' => 1))) as $p) {
     $p->rollCall();
   }
   $ben = Dbo::findOne('LostPerson', array('name' => 'Ben'));
   $locke = Dbo::findOne('LostPerson', array('name' => 'Locke'));
   $ben->enemy = Dbo::toRef($locke);
   $ben->goodguy = null;
   Dbo::save($ben);


If the 10gen folks can add a link, I'd be much obliged.

Best,

Ian White
Re: simplemongophp -- a simple library for Mongo PHP Eliot Horowitz 7/28/09 11:31 AM
Looks cool.  Added here:
http://www.mongodb.org/display/DOCS/PHP+Language+Center
A README for the github page would be really helpful (probably just
this post is good enough)
if you just put in a README file in the git repo, github will
automatically pull it in.