How to integrate ZF2 with Doctrine Mongo ODM?

I am trying to integrate zf2 beta3 with the mongo odm dogma (https://github.com/doctrine/DoctrineMongoODMModule) but not sucess.

How to install and configure it?

+6
source share
3 answers

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; /** @ODM\Document */ class User { /** @ODM\Id */ private $id; /** @ODM\Field(type="string") */ private $name; /** * @return the $id */ public function getId() { return $this->id; } /** * @return the $name */ public function getName() { return $this->name; } /** * @param field_type $id */ public function setId($id) { $this->id = $id; } /** * @param field_type $name */ 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(); } } 
+3
source

I'm doing the same thing. Something like this should work:

Download the module and place it in the vendor folder.

Add the module to application.config.php

Copy module.doctrine_mongodb.config.php.dist to / config / autoload

Edit this configuration file with your own settings.

Change the name of this configuration file to module.doctrine_mongodb.local.config.php

Create the setDocumentManager method in your controller as follows:

 protected $documentManager; public function setDocumentManager(DocumentManager $documentManager) { $this->documentManager = $documentManager; return $this; } 

Place the following in the configuration of the DI module:

  'Application\Controller\[YourControllerClass]' => array( 'parameters' => array( 'documentManager' => 'mongo_dm' ) ), 

Create the document classes according to the Doctrine 2 documentation and the explanations in this question and answer: Annotations Namespace not loaded DoctrineMongoODMModule for Zend Framework 2

Finally, use dm as follows:

 public function indexAction() { $dm = $this->documentManager; $user = new User(); $user->set('name', 'testname'); $user->set('firstname', 'testfirstname'); $dm->persist($user); $dm->flush(); return new ViewModel(); } 
+6
source

Now the default configuration has changed, can you show an updated method to make this work in ZF2?

 <?php return array( 'doctrine' => array( 'connection' => array( 'odm_default' => array( 'server' => 'localhost', 'port' => '27017', 'user' => null, 'password' => null, 'dbname' => 'user', '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' => null, 'filters' => array() // array('filterName' => 'BSON\Filter\Class') ) ), 'driver' => array( 'odm_default' => array( 'drivers' => array() ) ), 'documentmanager' => array( 'odm_default' => array( 'connection' => 'odm_default', 'configuration' => 'odm_default', 'eventmanager' => 'odm_default' ) ), 'eventmanager' => array( 'odm_default' => array( 'subscribers' => array() ) ), ), ); 

Currently received error: class "Application \ Document \ User" was not found in the chain of names with chain

0
source

Source: https://habr.com/ru/post/913881/


All Articles