2011-03-28 86 views
4

是否有任何知道使用Doctrine 2 beta ODM将zend框架与Mongo集成的方法? 我已经查看了与Doctrine 2 ORM for MySQL集成的zendcast视频,但是Bisna从未更新过支持Mongo。使用Doctrine ODM将Zend Framework 1.11与MongoDB集成

我想我可以试着破解比斯纳来让它工作,但我想知道别人是否已经找到了一种方法来让它工作。

回答

1

我写了我自己的很简单简单的应用程序资源插件和容器,使用Guilherme的集成套件的灵感。

我敢肯定,这可能在捕捉选项方面更具特色,但我想我会在需要时添加它们。

https://gist.github.com/891415

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 <[email protected]> 
*/ 
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

我不认为ZF 1.11支持名称空间的应用程序资源插件。顺便说一句,从一个同伴Whirlpooler你好:) – Phil 2011-03-28 22:49:37

+0

我没有改变Zend框架中的任何东西,所以我想它支持命名空间。 Whirlpool FTW :) – Cobby 2011-03-28 22:56:29

+0

你如何配置资源插件的路径?它是如何被调用的? – Phil 2011-03-28 23:04:55