如何将 ZF2 与 Doctrine Mongo ODM 集成? [英] How to integrate ZF2 with Doctrine Mongo ODM?
问题描述
我正在尝试将 zf2 beta3 与学说 mongo odm (https://github.com/doctrine/DoctrineMongoODMModule) 集成,但没有成功.
I am trying to integrate zf2 beta3 with doctrine mongo odm (https://github.com/doctrine/DoctrineMongoODMModule) but no sucess.
如何安装和配置它?
推荐答案
我将给出我为将 zf2 与 mongodb 学说 odm 集成所做的步骤
I will give the steps I have done to integrate zf2 with mongodb doctrine odm
1.下载mongodb Dotty odm 模块并放到vendor目录下或者从github克隆
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.从/path/to/project/vendor/DoctrineMongoODMModule/config/module.doctrine_mongodb.config.php.dist复制文件,放在你的路径/to/your/project/config/autoload/中并重命名为module.doctrine_mongodb.local.config.php
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.编辑你的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'
),
更改您的连接参数
'connection' => array(
//'server' => 'mongodb://<user>:<password>@<server>:<port>',
'server' => 'mongodb://localhost:27017',
'options' => array()
),
更改驱动程序选项
'driver' => array(
'class' => 'DoctrineODMMongoDBMappingDriverAnnotationDriver',
'namespace' => 'ApplicationDocument',
'paths' => array('module/Application/src/Application/Document'),
),
添加代理和 hydratos 配置
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' => 'ApplicationModelProxy',
'auto_generate_hydrators' => true,
'hydrator_dir' => __DIR__ . '/../../module/Application/src/Application/Document/Hydrators',
'hydrator_namespace' => 'ApplicationDocumentHydrators',
'default_db' => $settings['config']['default_db'],
),
'metadataCache' => $settings['cache'],
)
),
4.在/path/to/project/module/Application/src/Application/中创建一个名为Document"的目录,您的文档映射所在的位置,并在Document"目录中,创建Proxy"和Hydrators"目录.
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. 编辑您的/path/to/project/config/application.config.php 并将 'DoctrineMongoODMModule' 添加到 modules 数组
5.Edit your /path/to/project/config/application.config.php and add 'DoctrineMongoODMModule' to modules array
6.确保你已经安装了 mongo php 扩展,否则在 http://www.php.net/manual/en/mongo.installation.php#mongo.installation.windows 并将其复制到您的扩展 php 目录,通常是/php/ext.根据您下载的名称文件扩展名extension=php_mongo-x.x.x-5.x-vc9.dll"在php.ini中添加扩展行.
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.在你的文档目录应用模块中创建一个文档映射User.php.
7.Create a document mapping User.php in your document directory application module.
<?php
namespace ApplicationDocument;
use DoctrineODMMongoDBMappingAnnotations as ODM;
/** @ODMDocument */
class User
{
/** @ODMId */
private $id;
/** @ODMField(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,例如在你的控制器中
8.Persist it, for example in your controller
<?php
namespace ApplicationController;
use ZendMvcControllerActionController,
ZendViewModelViewModel,
ApplicationDocumentUser;
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();
}
}
这篇关于如何将 ZF2 与 Doctrine Mongo ODM 集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!