paolo@bimodesign.com | +34 608 61 64 10

Framework

        

Zend 2 - Doctrine 2 and MongoDb (Create)

In this post I'll explain how to integrate the Zend 2 with Doctrine 2 and MongoDB, inserting a simple record/document into a collection user (that doesn't exist).
After installing the Zend 2, Doctrine 2 and Mongo Db how to explain in the other my post, we'll starting to tell ZF2 that we have Doctrine and Doctrine MongoDB ODM Module, editing the file config/application.config.php:

return array(
    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Application',
        'User',
        'DoctrineModule',
        'DoctrineMongoODMModule',
    ),
    ...
    ... The other configuration
    ...

Then we copy the sample config file from the dir vendor/doctrine/doctrine-mongo-odm-module/config/ to the autoload directory and modify its name to module.doctrine-mongo-odm.local.php and edit it in this way (the database name is "test")

return array(
    'doctrine' => array(

        'connection' => array(
            'odm_default' => array(
                'server'    => 'localhost',
                'port'      => '27017',
                'dbname'    => 'test',
                'options'   => array()                
            ),
        ),

        'configuration' => array(
            'odm_default' => array(
                'metadata_cache'     => 'array',
                'driver'             => 'odm_default',
                'generate_proxies'   => true,
                'proxy_dir'          => 'data/DoctrineMongoODMModule/Proxy',
                'proxy_namespace'    => 'DoctrineMongoODMModule\Proxy',
                'generate_hydrators' => true,
                'hydrator_dir'       => 'data/DoctrineMongoODMModule/Hydrator',
                'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
                'default_db'         => test,
                'filters'            => array(),  // array('filterName' => 'BSON\Filter\Class'),
//                'logger'             => null // 'DoctrineMongoODMModule\Logging\DebugStack'
            )
        ),

        'driver' => array(
            'odm_default' => array(
                'drivers' => array(
                    'User\Document' => 'mongoPb'
                )
            ),
            'mongoPb' => array(
                'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(
                    'module/User/src/User/Document'
                )
            )
        ),
        'documentmanager' => array(
            'odm_default' => array(
                'connection'    => 'odm_default',
                'configuration' => 'odm_default',
                'eventmanager' => 'odm_default'
            )
        ),
        'eventmanager' => array(
            'odm_default' => array(
                'subscribers' => array()
            )
        ),
    ),
);

Now create this writable directories

mkdir -p data/DoctrineMongoODMModule/Hydrator
mkdir -p data/DoctrineMongoODMModule/Proxy

chmod -R 777 data/


Next step will be to define the document mapping into module/User/src/User/Document/User.php
Note that I use Document name directory in order to separate the MySQL model to the MongoDb model. But this is a simple example so you can configure it as you like.

Finally, add this code to the action create of the User controller:

    ...
    ...
    public function createAction()
    {
        $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');  
        $user = new User();
        $user->setName("Paolo");
        $dm->persist($user);
        $dm->flush();
        
        return new ViewModel();
    }
    ...
    ...

and execute it.

To show the data the dababase you can connect by command line or using a graphic software. Here I'm using the command line:

> show databases
blog   0.078GB
enron  0.953GB
local  0.078GB
test   0.078GB
> use test
switched to db test
> show collections
system.indexes
user
zips
> db.user.find()
{ "_id" : ObjectId("55b78999e0cf201b19f1696b"), "name" : "Paolo" }

In the next post I'll explain how to Edit and Delete a document.

Note: You can download the entire proyect from my github account.