O'Reilly logo

MongoDB and PHP by Steve Francia

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4. PHP Libraries and Tools

I hope that by now, you’ve become excited about how nice the MongoDB driver is to use. We at 10gen have worked extremely hard to make the developer experience as pleasant as possible.

While the MongoDB driver makes it quite easy to use MongoDB from within PHP, you may prefer to add another level of abstraction, particularly with larger applications. One reason for doing so would be to standardize document structure across the application, including data validation.

Many PHP libraries exist to work with MongoDB. I have selected only a handful that I have experience with and that I feel are viable solutions. This list isn’t meant to be exhaustive but rather to introduce the reader to libraries that may be able to help them with their projects. I am not endorsing any of these libraries—merely introducing them as potential solutions for the reader.

Object Document Mappers (ODM)

A number of solid Object Document Mappers (ODMs) exist for PHP. The concept is similar to the ORM, but the implementation is significantly different. The ODMs are considerably lighter than any ORM and much much faster. I’ve listed a few I have experience with here.

Note

Disclaimer: Doctrine development is sponsored by OpenSky, the social ecommerce website, where I worked as the VP of engineering. In spite of this relationship, I’ve endeavored to be as impartial as possible in my introduction to these libraries.

Doctrine MongoDB ODM

Doctrine was the first viable ORM for PHP. Unlike prior solutions, which followed the active record model, Doctrine took its inspiration from Hibernate for Java. Doctrine2 is an ambitious project that has resulted in a faster and more efficient library.

Concurrent with the development of Doctrine2, the NoSQL movement began to pick up steam, and as an extension of the Doctrine project, Doctrine MongoDB ODM was born.

Doctrine MongoDB ODM leverages the rich set of features Doctrine provides to enable things like validation and events. It allows you to create objects that transparently persist to MongoDB while retaining the same style of objects and behavior as the Doctrine ORM project. In fact, you can even create a single object that has elements persisted in each backend. OpenSky used this feature to use a hybrid of MongoDB and MySQL to add transactional support for our orders. See http://www.spf13.com/presentations for details.

Doctrine supports the following features:

  • Provides full validation

  • Seamless integration with Doctrine ORM

  • Useful for hybrid solutions

  • Follows same persistence model as Doctrine

  • Uses a Document Manager and annotations

  • Supports embedded and referenced objects

  • Can use mongo’s query interface

  • Supports in place updates

Doctrine MongoDB ODM can be found at https://github.com/doctrine/mongodb-odm.

Active Mongo

Active Mongo seeks to take the MongoDB interface—which returns arrays—even further, utilizing the Active Record paradigm. In addition to providing the friendly interface that comes with Active Record, one of the big features Active Mongo provides is automatically calculating the diff and performing in place updates. Many of the alternatives would set the entire document each time, whereas Active Mongo ensures that the minimal amount is changed.

Active Mongo supports a simple filter mechanism that it uses for validation.

Active Mongo supports the following features:

  • Uses Active Record (active document) interface

  • Supports full validation

  • Supports in place updating

  • Uses native query interface

  • Supports referenced documents

Active Mongo can be found at https://github.com/crodas/ActiveMongo.

Mandango

Mandango was built for speed. Specifically, it was written to be as simple and fast as possible. Given the lean approach, it boasts a fairly significant feature set:

  • Very light and fast

  • Event support

  • Supports embedded and referenced objects

  • Uses the mongo query syntax

  • Lacking validation, but events can be utilized to validate

Mandango is located at https://github.com/mandango/mandango.

Tools

Various tools exist for MongoDB. I’ve only included ones here that are PHP based, but as they provide support and operate independent of your application, they really could be any language.

MongoQueue

MongoQueue is an (asynchronous) queuing system using nothing but PHP and MongoDB. It’s an excellent and popular use case for MongoDB and a great fit for PHP.

MongoQueue supports the following features:

  • Fully configurable

  • Distributed

  • Atomic locking

  • Priority support

  • Worker timeout support

  • Stable

MongoQueue can be found at https://github.com/skiz/mongo_queue.

Genghis

Genghis is a PHP frontend for MongoDB, similar in nature to PHPMyAdmin (see Figure 4-1). Genghis boasts single-file installation and a very usable interface.

Genghis edit screen
Figure 4-1. Genghis edit screen

Genghis can be found at https://genghisapp.com.

RockMongo

RockMongo is similar to Genghis except that it is more mature and isn’t quite as pretty. RockMongo can be found at http://code.google.com/p/rock-php/wiki/rock_mongo.

Frameworks

As many projects today are not written from scratch, but rather jump-started by using an existing framework, I’ll briefly describe the current state of frameworks as it pertains to MongoDB. As counsel, many factors should go into choosing which framework to use for a project and support for a specific database, while it may be a factor, shouldn’t be the only factor.

Even if a given framework doesn’t explicitly support MongoDB, don’t view this as incompatibility. In a discussion with Paul Jones, the author of the Solar framework (and a fan of MongoDB), I asked him if they had plans to support MongoDB. His answer (parahprased): “Why? The driver does so much that I don’t think we could add to it to make the experience any better.” He’s not alone; I know of a few other frameworks that have opted to stick with the driver without adding any additional support because it works so well.

The following frameworks have built support for MongoDB internally. Often it is not only to use it as a core data store, but to integrate it with additional components, such as as a backend for session handling.

Symfony2

Philosophically, Symfony2 believes you should be able to use what you want and provides a wonderful plugin architecture to permit you to do so. Sensio Labs, the parent of the Symfony project, also sponsors the Doctrine project and the two teams have a decent amount of overlap. Doctrine2 works quite well with Symfony2, providing both RDBMS and MongoDB support.

Lithium

Lithium is the first framework to boast support for NoSQL solutions right out of the box. In fact, originally it only supported MongoDB and CouchDB. The Lithium project was started by Nate Abele, the former lead developer of the CakePHP framework.

Zend

Zend, the PHP company, has been developing its own framework for a few years. It is widely used, and through the Shanty Mongo plugin, has MongoDB support. Shanty Mongo is an ODM like many of the libraries mentioned here. There is currently a proposal for MongoDB to have full support for an upcoming release of Zend Framework. Additionally, a number of components will support MongoDB in upcoming releases.

Fuel

Fuel supports MongoDB out of the box. It provides a simple wrapper that isn’t any simpler than the driver.

FatFree Framework

The FatFree Framework—or F3, as it’s commonly called—provides M2, the Mongo Mapper library, to interface with MongoDB.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required