yii\mongodb\Connection::open PHP Method

open() public method

It does nothing if a MongoDB connection has already been established.
public open ( )
    public function open()
    {
        if ($this->manager === null) {
            if (empty($this->dsn)) {
                throw new InvalidConfigException($this->className() . '::dsn cannot be empty.');
            }
            $token = 'Opening MongoDB connection: ' . $this->dsn;
            try {
                Yii::trace($token, __METHOD__);
                Yii::beginProfile($token, __METHOD__);
                $options = $this->options;
                $this->manager = new Manager($this->dsn, $options, $this->driverOptions);
                $this->manager->selectServer($this->manager->getReadPreference());
                $this->initConnection();
                Yii::endProfile($token, __METHOD__);
            } catch (\Exception $e) {
                Yii::endProfile($token, __METHOD__);
                throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
            }
            $this->typeMap = array_merge($this->typeMap, ['root' => 'array', 'document' => 'array']);
        }
    }

Usage Example

Exemplo n.º 1
0
 public function testOpenClose()
 {
     $connection = $this->getConnection(false, false);
     $this->assertFalse($connection->isActive);
     $this->assertEquals(null, $connection->mongoClient);
     $connection->open();
     $this->assertTrue($connection->isActive);
     $this->assertTrue(is_object($connection->mongoClient));
     $connection->close();
     $this->assertFalse($connection->isActive);
     $this->assertEquals(null, $connection->mongoClient);
     $connection = new Connection();
     $connection->dsn = 'unknown::memory:';
     $this->setExpectedException('yii\\mongodb\\Exception');
     $connection->open();
 }
All Usage Examples Of yii\mongodb\Connection::open