simplemongophp -- a simple library for Mongo PHP

565 views
Skip to first unread message

Ian White

unread,
Jul 28, 2009, 1:52:33 AM7/28/09
to mongod...@googlegroups.com
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
Reply all
Reply to author
Forward
Message has been deleted
0 new messages