2011-03-02 33 views
5

我想Symfony的2 MongoDB中以这样的方式连接:的Symfony2 + DoctrineMongoDBBundle配置

  1. 注册DoctrineMongoDBBundleAppKernel::registerBundles 方法
  2. 设置 'doctrine_mongo_db' 配置(见下文config.yml
  3. 从容器中获取'doctrine.odm.mongodb.document_manager' HelloController动作

而当我试图运行应用程序MongoConnectionException被引发。

任何人都可以帮助我解决这个问题吗?


AppKernel.php

public function registerBundles() 
{ 
     $bundles = array(
      new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), 
      new Symfony\Bundle\TwigBundle\TwigBundle(), 
      new Symfony\Bundle\DoctrineMongoDBBundle\DoctrineMongoDBBundle(), 
      new Sensio\HelloBundle\HelloBundle() 
     ); 

     return $bundles; 
    } 

config.yml

framework: 
    charset:  UTF-8 
    router:  { resource: "%kernel.root_dir%/config/routing.yml" } 
    templating: { engines: ['twig'] } 

## Doctrine Configuration 

doctrine_mongo_db: 
    server: mongodb://root:[email protected]:27017 
    default_database: test 
    options: { connect: true } 
    mappings: 
     HelloBundle: { type: annotation, dir: Document } 

# Twig Configuration 

twig: 
    debug:   %kernel.debug% 
    strict_variables: %kernel.debug% 

HelloController.php

/* @var $dm \Doctrine\ODM\MongoDB\DocumentManager */ 
$dm = $this->get('doctrine.odm.mongodb.document_manager'); 

的异常(线96)

connecting to failed: Transport endpoint is not connected 

in ~/vendor/doctrine-mongodb/lib/Doctrine/MongoDB/Connection.php line 96 » 

93. if ($this->server) { 
94.  $this->mongo = new \Mongo($this->server, $this->options); 
95. } else { 
96.  $this->mongo = new \Mongo(); 
97. } 
+2

经过一番研究,我发现问题出在DoctrineMongoDBBundle :: load方法中。在传递给DoctrineMongoDBBundle :: doMongodbLoad方法之前,应该合并$ configs。它看起来像一个错误。 – 2011-03-03 00:01:30

+2

是的,我说得对。这些家伙现在正在重写DoctrineMongoDBBundle配置。 https://github.com/fabpot/symfony/pull/740 – 2011-03-03 00:33:33

+3

我认为你应该将该信息作为答案发布并接受;) – 2011-11-18 09:06:09

回答

1

的问题是在DoctrineMongoDBBundle配置加载。修复(https://github.com/fabpot/symfony/pull/740)应尽快合并。

现在您可以使用下面的固定方法。

public function load(array $configs, ContainerBuilder $container) 
{ 
    $mergedConfig = array(); 
    foreach ($configs as $config) { 
     $mergedConfig = array_merge_recursive($mergedConfig, $config); 
    } 

    $this->doMongodbLoad($mergedConfig, $container); 
}