ZF2与Doctrine Mongo ODM如何整合? [英] How to integrate ZF2 with Doctrine Mongo ODM?
问题描述
我正在尝试将zf2 beta3与教条mongo odm(https://github.com/doctrine/DoctrineMongoODMModule)整合,但没有成功。
如何安装和配置?
给出我已经完成的步骤,以将zf2与mongodb doctrine odm
整合1.下载mongodb doctrine odm模块并放在供应商目录中,或从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,放在你的路径/到/ your / project / config / autoload /并重命名为module.doctrine_mongodb.local.config.php
更改默认db
'config'=>数组(
pre>
//设置要使用的默认数据库(或不))
'default_db'=>'myDbName'
),
更改连接参数
'connection'=>数组(
//'server'=>'mongodb://< user>:< password> @< server>:< port>',
'server'=> 'mongodb:// localhost:27017',
'options'=> array()
),
更改驱动程序选项
'driver'=> array(
'class'=>'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'namespace'=>'Application\Document',
'paths'=>数组('module / Application / src / Application / Document'),
),
添加代理和hydratros配置
'mongo_config'=>数组(
'parameters'=>数组(
'opts'=>数组(
'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\文件\Hydrators',
'default_db'=> $ settings ['config'] ['default_db'],
),
'metadataCache'=> $ settings ['cache '],
)
),
4.创建一个名为在/ path / to / project / module / Application / src / Application /里面的Document文件映射和Document目录下,创建Proxy和Hydrators目录。
5.编辑/path/to/project/config/application.config.php,并将DoctrineMongoODMModule添加到modules数组
6.Be确保你安装了mongo php扩展名,否则请在 http:/ /www.php.net/manual/en/mongo.installation.php#mongo.installation.windows ,并将其复制到扩展名php目录,通常是/ php / ext。在您的php.ini中添加您已下载的名称文件扩展名的扩展名extension = php_mongo-xxx-5.x-vc9.dll。
7。在文档目录应用程序模块中创建映射User.php的文档。
<?php
命名空间Application\Document;
使用Doctrine\ODM\MongoDB\Mapping\Annotations作为ODM;
/ ** @ ODM\Document * /
class User
{
/ ** @ ODM\Id * /
private $ id;
/ ** @ ODM\Field(type =string)* /
private $ name;
/ **
* @返回$ id
* /
public function getId(){
return $ this-> id;
}
/ **
* @返回$ name
* /
public function getName(){
return $ this- >名称;
}
/ **
* @param field_type $ id
* /
public function setId($ id){
$ this - > id = $ id;
}
/ **
* @param field_type $ name
* /
public function setName($ name){
$ this - > name = $ name;
}
}
8.支持它,例如在您的控制器中
<?php
命名空间Application\Controller;
使用Zend\Mvc\Controller\ActionController,
Zend\View\Model\ViewModel,
Application\Document\User;
class IndexController extends ActionController
{
public function indexAction()
{
$ dm = $ this-> getLocator() - >获得( 'mongo_dm');
$ user = new User();
$ user-> setName('Bulat S.');
$ dm-> persist($ user);
$ dm-> flush();
返回新的ViewModel();
}
}
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 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(); } }
这篇关于ZF2与Doctrine Mongo ODM如何整合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!