Symfony\Bundle\DoctrineBundle\Registry::getConnection PHP Method

getConnection() public method

Gets the named connection.
public getConnection ( string $name = null ) : Doctrine\DBAL\Connection
$name string The connection name (null for the default one)
return Doctrine\DBAL\Connection
    public function getConnection($name = null)
    {
        if (null === $name) {
            $name = $this->defaultConnection;
        }

        if (!isset($this->connections[$name])) {
            throw new \InvalidArgumentException(sprintf('Doctrine Connection named "%s" does not exist.', $name));
        }

        return $this->container->get($this->connections[$name]);
    }

Usage Example

Example #1
0
 public function testGetUnknownConnection()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $registry = new Registry($container, array(), array(), 'default', 'default');
     $this->setExpectedException('InvalidArgumentException', 'Doctrine Connection named "default" does not exist.');
     $registry->getConnection('default');
 }