yii\db\Connection::open PHP Method

open() public method

It does nothing if a DB connection has already been established.
public open ( )
    public function open()
    {
        if ($this->pdo !== null) {
            return;
        }
        if (!empty($this->masters)) {
            $db = $this->openFromPool($this->masters, $this->masterConfig);
            if ($db !== null) {
                $this->pdo = $db->pdo;
                return;
            } else {
                throw new InvalidConfigException('None of the master DB servers is available.');
            }
        }
        if (empty($this->dsn)) {
            throw new InvalidConfigException('Connection::dsn cannot be empty.');
        }
        $token = 'Opening DB connection: ' . $this->dsn;
        try {
            Yii::info($token, __METHOD__);
            Yii::beginProfile($token, __METHOD__);
            $this->pdo = $this->createPdoInstance();
            $this->initConnection();
            Yii::endProfile($token, __METHOD__);
        } catch (\PDOException $e) {
            Yii::endProfile($token, __METHOD__);
            throw new Exception($e->getMessage(), $e->errorInfo, (int) $e->getCode(), $e);
        }
    }

Usage Example

 public function initialize()
 {
     require_once "vendor/autoload.php";
     define('YII_DEBUG', false);
     require_once "vendor/yiisoft/yii2/Yii.php";
     $application = new yii\console\Application(['id' => 'yii-benchmark', 'basePath' => __DIR__]);
     Author::$db = Book::$db = $this->db = new Connection(['dsn' => 'sqlite::memory:']);
     $this->db->open();
     Author::createTable();
     Book::createTable();
 }
All Usage Examples Of yii\db\Connection::open