开发者

Symfony2 + DoctrineMongoDBBundle Configuration

开发者 https://www.devze.com 2023-02-14 10:03 出处:网络
I am trying to connect Symfony 2 with MongoDB in such way: Register DoctrineMongoDBBundle in AppKernel::registerBundles

I am trying to connect Symfony 2 with MongoDB in such way:

  1. Register DoctrineMongoDBBundle in AppKernel::registerBundles method
  2. Set 'doctrine_mongo_db' configuration (see below config.yml)
  3. Get 'doctrine.odm.mongodb.document_manager' from container in HelloController action

And when I am trying to run the application MongoConnectionException is thrown.

Can anyone help me to solve this problem?

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:root@192.168.0.111:27017
    default_database: test
    options:开发者_C百科 { 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');

Exception (line 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.    }


The problem is in DoctrineMongoDBBundle configuration loading. The fix (https://github.com/fabpot/symfony/pull/740) should be merged soon.

For now you can use fixed method below.

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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号