Doctrine is one of the coolest Open Source PHP database frameworks of recent years. Their MongoDB ODM project is still in Beta development but having used it recently I have to say it is really, really good. I’ve been integrating it with Zend Framework recently and have developed a handy container class that makes integration simple. The project is hosted on Google code and I will give an overview here on how to use it.

Download the latest version of my library and place the directories inside your directory. For example;

application
public
bin
library
--Doctrine
--Wildkat
--Zend

Configuration

If you look at the example application.ini, you’ll notice all configuration is placed under the ‘doctirne’ root.

There are 3 section that you need to configure (cache, connection and document managers).

; --- @ Configure Cache instances @ ---
doctrine.cache.default.driver = Doctrine\Common\Cache\ArrayCache
doctrine.cache.default.namespace = 'Wildkat'
;doctrine.cache.memcache.driver = Doctrine\Common\Cache\MemcacheCache
;doctrine.cache.memcache.servers.0.host = 127.0.0.1
;doctrine.cache.memcache.servers.0.port = 11211


; --- @ Configure connections @ ---
doctrine.connection.default.cache = default
doctrine.connection.default.host = localhost
doctrine.connection.default.port = 27017
;doctrine.connection.default.connect = true
;doctrine.connection.default.replicaSet = true
;doctrine.connection.default.persist = 'wildkat_presist'
;doctrine.connection.default.username =
;doctrine.connection.default.password =
;doctrine.connection.default.mongoCmd = $
;doctrine.connection.default.loggerClass =
;doctrine.connection.default.loggerCallback =

; --- @ Configure the document manager instances @ ---
doctrine.dms.default.connection = default
doctrine.dms.default.autoGenerateHydrationClasses = false
doctrine.dms.default.autoGenerateProxyClasses = false
doctrine.dms.default.documentNamespaces[] = Wildkat\Documents
doctrine.dms.default.hydratorDir = /tmp
doctrine.dms.default.hydratorNamespace = Wildkat
doctrine.dms.default.metadataCache = default
doctrine.dms.default.metadataDir = /tmp
doctrine.dms.default.proxyDir = /tmp
doctrine.dms.default.proxyNamespace = WildkatProxy
doctrine.dms.default.metadataDriver = AnnotationDriver
;doctrine.dms.default.defaultDb =

For detailed information on what each parameter does, consult the documentation or look around the sourcecode. Most of the ini lines are access to the setters of each component so for example autoGenerateProxyClasses = false will invoke setAutoGenerateProxyClasses(false).

Bootstrap

In your bootstrap, create a new init method that instantiates the container:

use Wildkat\Application\Container\DoctrineContainer;

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Loads the container
     *
     * @return Wildkat\Application\Container\DoctrineContainer
     */
    public function _initDoctrineMongoContainer()
    {
        $container = new DoctrineContainer($this->getOption('doctrine'));
        Zend_Registry::set('Wildkat\DoctrineContainer', $container);

        return $container;
    }
}

You can the obtain the container in a number of ways. You can access it via the registry by using the registry:

Zend_Registry::get('Wildkat\DoctrineContainer')

or via the bootstrap

$bootstrap->getResource('DoctrineMongoContainer');

Using the container

Once you have the container object there are three methods you’ll want to use.

$dm = $container->getDocumentManager('default');
$cache = $container->getCacheInstance('default');
$conn = $container->getConnection('default');

The ‘default’ word in each method parameter represents the sections specified in your application.ini.

Command Line Tool

Once you have everything configured, the command line tool will work with the container. The tool is located inside the bin directory. The format for invoking the cli tool is as follows;

php -q doctrinemongo.php [dm name e.g. 'default'] [cli commands]

This container is by no means complete but does covers most usage scenarios when working with MongoDB.

Advertisement