I am trying to integrate zf2 beta3 with doctrine mongo odm (https://github.com/doctrine/DoctrineMongoODMModule) but no sucess.
How can I install and configure it?
I'm doing just the same thing. Something like this should work:
Download the module, and place in your vendor folder.
Add the module in application.config.php
Copy module.doctrine_mongodb.config.php.dist to /config/autoload
Edit that config file with your own settings
Change the name of that config file to module.doctrine_mongodb.local.config.php
Create a 'setDocumentManager' method in your controller like this:
protected $documentManager;
public function setDocumentManager(DocumentManager $documentManager)
{
$this->documentManager = $documentManager;
return $this;
}
Place the following in your module's DI config:
'Application\Controller\[YourControllerClass]' => array(
'parameters' => array(
'documentManager' => 'mongo_dm'
)
),
Create Document classes according to the Doctrine 2 documentation, and the clarification in this question and answer: Annotations Namespace not loaded DoctrineMongoODMModule for Zend Framework 2
Finally, use the dm like this:
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();
}
I will give the steps I have done to integrate zf2 with mongodb doctrine odm
1.Download the mongodb doctrine odm module and place in vendor 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, place in your path/to/your/project/config/autoload/ and rename to module.doctrine_mongodb.local.config.php
3.Edit your module.doctrine_mongodb.local.config.php. Change the default db
'config' => array(
// set the default database to use (or not)
'default_db' => 'myDbName'
),
Change your connection params
'connection' => array(
//'server' => 'mongodb://<user>:<password>@<server>:<port>',
'server' => 'mongodb://localhost:27017',
'options' => array()
),
Change the driver options
'driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'namespace' => 'Application\Document',
'paths' => array('module/Application/src/Application/Document'),
),
Add proxy and hydratros config
'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 named "Document" in /path/to/project/module/Application/src/Application/ where goes your documents mapping and inside "Document" directory, create "Proxy" and "Hydrators" directories.
5.Edit your /path/to/project/config/application.config.php and add 'DoctrineMongoODMModule' to modules array
6.Be sure you have mongo php extension installed otherwise download at http://www.php.net/manual/en/mongo.installation.php#mongo.installation.windows and copy it to your extension php directory, usually /php/ext. Add the extension line acording the name file extension you have downloaded "extension=php_mongo-x.x.x-5.x-vc9.dll" in your php.ini.
7.Create a document mapping User.php in your document directory 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.Persist it, for example in 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();
}
}
Now the default config has changed, could you show an updated method to get this working 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 receiving the error: The class 'Application\Document\User' was not found in the chain configured namespaces
来源:https://stackoverflow.com/questions/10273130/how-to-integrate-zf2-with-doctrine-mongo-odm