Zend framework 如何将ZF2与Doctrine Mongo ODM集成?

Zend framework 如何将ZF2与Doctrine Mongo ODM集成?,zend-framework,doctrine,doctrine-orm,zend-framework2,doctrine-odm,Zend Framework,Doctrine,Doctrine Orm,Zend Framework2,Doctrine Odm,我正在尝试将zf2 beta3与mongo odm整合(https://github.com/doctrine/DoctrineMongoODMModule)但是没有成功 如何安装和配置它?我也在做同样的事情。像这样的方法应该会奏效: 下载模块,并将其放入供应商文件夹中 在application.config.php中添加模块 将module.doctor\u mongodb.config.php.dist复制到/config/autoload 使用您自己的设置编辑该配置文件 将该配置文件的名称

我正在尝试将zf2 beta3与mongo odm整合(https://github.com/doctrine/DoctrineMongoODMModule)但是没有成功


如何安装和配置它?

我也在做同样的事情。像这样的方法应该会奏效:

下载模块,并将其放入供应商文件夹中

在application.config.php中添加模块

将module.doctor\u mongodb.config.php.dist复制到/config/autoload

使用您自己的设置编辑该配置文件

将该配置文件的名称更改为module.doctor\u mongodb.local.config.php

在控制器中创建“setDocumentManager”方法,如下所示:

protected $documentManager;

public function setDocumentManager(DocumentManager $documentManager)
{
    $this->documentManager = $documentManager;
    return $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();
} 
将以下内容放入模块的DI配置中:

    'Application\Controller\[YourControllerClass]' => array(
        'parameters' => array(
            'documentManager' => 'mongo_dm'
        )
    ),
根据条令2文档和本问答中的说明创建文档类:

最后,像这样使用dm:

protected $documentManager;

public function setDocumentManager(DocumentManager $documentManager)
{
    $this->documentManager = $documentManager;
    return $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();
} 

我将给出将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/复制到/project/vendor/DoctrineMongoODMModule/config/module.doctrine\u mongodb.config.php.dist,将文件放在路径/to/your/project/config/autoload/中,并重命名为module.doctrine\u mongodb.local.config.php

3.编辑module.doctor\u mongodb.local.config.php。 更改默认数据库

'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()
),
添加代理和配置

        '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.在/path/to/project/module/Application/src/Application/where-goes-your-documents映射中创建一个名为“Document”的目录,并在“Document”目录中创建“Proxy”和“Hydrators”目录

5.编辑/path/to/project/config/application.config.php并将“DoctrineMongoODMModule”添加到模块数组中

6.确保已安装mongo php扩展名,否则请下载并复制到扩展名php目录,通常为/php/ext。根据php.ini中下载的文件扩展名“extension=php_mongo-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;
    }

}

现在默认配置已更改,您能否显示一个更新的方法以使其在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()
            )
        ),
    ),
);

Note
module.document\u mongodb.config.local.php
应该是
module.document\u mongodb.local.config.php
嗨,这对我帮助很大,谢谢。我现在让它工作了。配置有点复杂,我将在这里发布一个完整的答案来配置它,以帮助新手。我能够让文档持久化,但检索文档时遇到困难。你做到了吗?@Zoop Josh你有什么麻烦?是不是发现了错误?是因为发现了错误。然而,自从你提出来后,我重新检查了你的说明,它就在那里;更新路径!谢谢