Yii2 得到当前数据库的名字 get mysql db name:
public function getCurrentDbName()
{
$db = Yii::$app->getDb();
$dbName = $this->getDsnAttribute('dbname', $db->dsn);
return $dbName;
}
private function getDsnAttribute($name, $dsn)
{
if (preg_match('/' . $name . '=([^;]*)/', $dsn, $match)) {
return $match[1];
} else {
return null;
}
}