如何将ZF2与Doctrine Mongo ODM整合?
我想ZF2素β3教义蒙戈ODM(https://github.com/doctrine/DoctrineMongoODMModule),但没有sucess整合。如何将ZF2与Doctrine Mongo ODM整合?
如何安装和配置呢?
我会给我所做的ZF2与MongoDB的学说ODM整合步骤
1.Download MongoDB的学说ODM模块和发生在供应商目录,或者从GitHub克隆它
cd /path/to/project/vendor
git clone --recursive https://github.com/doctrine/DoctrineMongoODMModule.git
2.复印文件从/路径/到/项目/供应商/ DoctrineMongoODMModule /配置/模块。 doctrine_mongodb.config.php.dist,放在你的路径/ to/your/project/config/autoload /并重命名为module.doctrine_mongodb.local.config.php
3.编辑你的module.doctrine_mongodb.local.config .PHP。 更改默认的数据库
'config' => array(
// set the default database to use (or not)
'default_db' => 'myDbName'
),
更改连接PARAMS
'connection' => array(
//'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' => array('module/Application/src/Application/Document'),
),
添加代理和hydratros配置
'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'],
)
),
.在/ path/to/project/module/Application/src/Application /目录中创建一个名为“Document”的目录,在其中创建“Proxy”和“Hydrators”目录。
5.Edit您/path/to/project/config/application.config。PHP和添加“DoctrineMongoODMModule”到模块阵列
6.Be确保已安装蒙戈PHP扩展,否则下载的http://www.php.net/manual/en/mongo.installation.php#mongo.installation.windows并将它复制到你的扩展PHP目录,通常是/ PHP /分机。在你的php.ini中添加你下载的扩展名“extension = php_mongo-x.x.x-5.x-vc9.dll”的扩展名。
7.在文档目录应用程序模块中创建一个映射User.php的文档。
<?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它,例如在你的控制器
<?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();
}
}
我做一样的事情。像这样的东西应该工作:在您的供应商目录
下载的模块和地点。
添加模块application.config.php
复制module.doctrine_mongodb.config.php.dist到/配置/自动加载
编辑与您自己的设置配置文件
更改该配置文件的名称module.doctrine_mongodb.local.config.php
在控制器这样创建“setDocumentManager”的方法:
protected $documentManager;
public function setDocumentManager(DocumentManager $documentManager)
{
$this->documentManager = $documentManager;
return $this;
}
放置在你的模块的DI配置如下:
'Application\Controller\[YourControllerClass]' => array(
'parameters' => array(
'documentManager' => 'mongo_dm'
)
),
根据教义2文档创建文档类和澄清这个问题,答案是:Annotations Namespace not loaded DoctrineMongoODMModule for Zend Framework 2
最后,使用DM像这样:
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();
}
注'module.doctrine_mongodb.config.local.php'应该是没有被发现'module.doctrine_mongodb .local.config.php' – 2012-04-25 00:37:10
嗨,它帮了我很多,谢谢。我现在开始工作了。配置有点复杂,我会在这里发布一个完整的配置来帮助新手。 – dextervip 2012-04-25 03:55:09
现在默认的配置发生了变化,你能表现出一个更新的方法来获得在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()
)
),
),
);
目前收到的错误:类“应用程序\文献\用户”链中的配置命名空间
我可以让文件保留,但我无法检索文件。你设法做到了吗? – 2012-05-02 02:37:56
@ Zoop-Josh你有什么麻烦?水合物未找到错误? – dextervip 2012-05-03 02:05:57
这是水化器错误。但是,既然你提起了它,我重新检查了你的指示,它就在那里;更新水化器路径!谢谢! – 2012-05-03 07:40:22