使用Doctrine ODM将Zend Framework 1.11与MongoDB集成

时间:2011-03-28 15:19:46

标签: zend-framework mongodb doctrine doctrine-orm

有没有人知道使用Doctrine 2 beta ODM将zend框架与Mongo集成的方法? 我已经看过关于与Doctrine 2 ORM for MySQL集成的zendcast视频,但Bisna从未更新为支持Mongo。

我想我可以试着破解Bisna让它运转起来,但我想知道是否其他人已经找到了让它运转的方法。

2 个答案:

答案 0 :(得分:7)

编写Zend Bootstrap Resource非常容易。

这是我使用的一个:

<?php

namespace Cob\Application\Resource;

use Doctrine\Common\Annotations\AnnotationReader,
    Doctrine\ODM\MongoDB\DocumentManager,
    Doctrine\MongoDB\Connection,
    Doctrine\ODM\MongoDB\Configuration,
    Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver,
    Doctrine\Common\EventManager;

/**
 * Creates a MongoDB connection and DocumentManager instance
 *
 * @author Andrew Cobby <cobby@cobbweb.me>
 */
class Mongo extends \Zend_Application_Resource_ResourceAbstract
{

    /**
     * @return \Doctrine\ODM\MongoDB\DocumentManager
     */
    public function init()
    {
        $options = $this->getOptions() + array(
            'defaultDB'          => 'my_database',
            'proxyDir'          => APPLICATION_PATH . '/domain/Proxies',
            'proxyNamespace'    => 'Application\Proxies',
            'hydratorDir'       => APPLICATION_PATH . '/domain/Hydrators',
            'hydratorNamespace' => 'Application\Hydrators'
        );

        $config = new Configuration();
        $config->setProxyDir($options['proxyDir']);
        $config->setProxyNamespace($options['proxyNamespace']);
        $config->setHydratorDir($options['hydratorDir']);
        $config->setHydratorNamespace($options['hydratorNamespace']);
        $config->setDefaultDB($options['defaultDB']);

        $reader = new AnnotationReader();
        $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
        $config->setMetadataDriverImpl(new AnnotationDriver($reader, $this->getDocumentPaths()));

        $evm = new EventManager();
        $evm->addEventSubscriber(new SlugSubscriber());

        return DocumentManager::create(new Connection(), $config, $evm);
    }

    public function getDocumentPaths()
    {
        $paths = array();
        foreach(new \DirectoryIterator(APPLICATION_PATH . '/modules') as $module){
            $path = $module->getPathname() . '/src/Domain/Document';

            if((!$module->isDir() || $module->isDot()) || !is_dir($path)){
                continue;
            }

            $paths[] = $path;
        }

        if(!count($paths)){
            throw new \Exception("No document paths found");
        }

        return $paths;
    }

}

虽然您必须更新getDocumentPaths()方法以适合您的应用程序目录结构。

答案 1 :(得分:1)

我使用Guilherme的集成套件编写了我自己的非常简单的应用程序资源插件和容器。

我确信在捕获选项方面这可能更具特色,但我想我会在需要的时候添加它们。

请参阅https://gist.github.com/891415

相关问题