I will give the steps that I have taken to integrate zf2 with the dogma of the doctrine of mongodb
1.Download the odm module of the mongodb doctrine and put it in the supplier directory or clone it from github
cd /path/to/project/vendor git clone --recursive https://github.com/doctrine/DoctrineMongoODMModule.git
2. Copy the file from /path/to/project/vendor/DoctrineMongoODMModule/config/module.doctrine_mongodb.config.php.dist, put in your path / in / your / project / config / autoload / and rename module.doctrine_mongodb.local .config.php
3. Modify your module.doctrine_mongodb.local.config.php. Change db default value
'config' => array( // set the default database to use (or not) 'default_db' => 'myDbName' ),
Change connection settings
'connection' => array( //'server' => 'mongodb://<user>:<password>@<server>:<port>', 'server' => 'mongodb://localhost:27017', 'options' => array() ),
Change driver settings
'driver' => array( 'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver', 'namespace' => 'Application\Document', 'paths' => array('module/Application/src/Application/Document'), ),
Add proxy and hydratron configuration
'mongo_config' => array( 'parameters' => array( 'opts' => array( 'auto_generate_proxies' => true, 'proxy_dir' => __DIR__ . '/../../module/Application/src/Application/Document/Proxy', 'proxy_namespace' => 'Application\Model\Proxy', 'auto_generate_hydrators' => true, 'hydrator_dir' => __DIR__ . '/../../module/Application/src/Application/Document/Hydrators', 'hydrator_namespace' => 'Application\Document\Hydrators', 'default_db' => $settings['config']['default_db'], ), 'metadataCache' => $settings['cache'], ) ),
4. Create a directory called "Document" in / path / to / project / module / Application / src / Application /, where your documents are mapped and inside the "Document" directory, create the "Proxies" and "Hydrators" directories.
5. Modify your /path/to/project/config/application.config.php and add the 'DoctrineMongoODMModule' to the modules array
6. Make sure you have the mongo php extension installed, otherwise download http://www.php.net/manual/en/mongo.installation.php#mongo.installation.windows and copy it to the php extension directory, usually / php / ext. Add the extension line that contains the extension of the name of the file that you downloaded "extension = php_mongo-xxx-5.x-vc9.dll" to php.ini.
7.Create User.php document mapping in your document catalog application module.
<?php namespace Application\Document; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; class User { private $id; private $name; public function getId() { return $this->id; } public function getName() { return $this->name; } public function setId($id) { $this->id = $id; } public function setName($name) { $this->name = $name; } }
8. Transfer it, for example, to your controller
<?php namespace Application\Controller; use Zend\Mvc\Controller\ActionController, Zend\View\Model\ViewModel, Application\Document\User; class IndexController extends ActionController { public function indexAction() { $dm = $this->getLocator()->get('mongo_dm'); $user = new User(); $user->setName('Bulat S.'); $dm->persist($user); $dm->flush(); return new ViewModel(); } }