我需要知道 symfony 项目内的数据库名称和数据库服务器名称。如何在 symfony 中以编程方式访问当前数据库连接设置(使用 Doctrine)?
假设您的 EntityManager 为
$this->em
从 Symfony2 获取 Doctrine 数据库名称:
$this->em->getConnection()->getDatabase();
从 Symfony2 获取 Doctrine 主机名(服务器名称):
$this->em->getConnection()->getHost();
您还可以从连接访问许多其他参数,例如
username
、port
和 password
。请参阅连接类了解更多信息
例如:
foreach(Doctrine_Manager::getInstance()->getConnections() as $connection){
$conn = $connection->getOptions();
preg_match('/host=(.*);/', $conn['dsn'], $host);
var_dump($host);
}
特定 conexion 的 dsn 中的数据库名称:
数据库.yml
all:
conexion1:
class: sfDoctrineDatabase
param:
dsn: 'mysql:host=localhost;dbname=basegestion1'
username: miusuario
password: ********
conexion2:
class: sfDoctrineDatabase
param:
dsn: 'mysql:host=localhost;dbname=baseestadisticas1'
username: miusuario
password: ********
在行动中:
$mConexion1Options = Doctrine_Manager::getInstance()->getConnection('conexion1')->getOptions();
preg_match('/dbname=(.*)/', $mConexion1Options['dsn'], $mDbConexion1);
那么,数据库名称是:
echo $mDbConexion1[1]; //basegestion1
使用此代码:
$myConnection = Doctrine_Manager::getInstance()->getConnection('doctrine')->getOptions();
$dsnInfo = $this->parseDsn($myConnection['dsn']);
$settings = array();
$settings['dbUser'] = (string) $myConnection["username"];
$settings['dbPassword'] = (string) $myConnection["password"];
$settings['dbHost'] = (string) $dsnInfo["host"];
$settings['dbName'] = (string) $dsnInfo['dbname'];
private function parseDsn ($dsn)
{
$dsnArray = array();
$dsnArray['phptype'] = substr($dsn, 0, strpos($dsn, ':'));
preg_match('/dbname = (\w+)/', $dsn, $dbname);
$dsnArray['dbname'] = $dbname[1];
preg_match('/host = (\w+)/', $dsn, $host);
$dsnArray['host'] = $host[1];
return $dsnArray;
}
现代(截至 2022 年 10 月)Doctrine DBAL 3.4.5 和 Persistence 2.5.4 的答案
在 Doctrine 的现代迭代中,我只能找到一种通过注入
doctrine.yaml
来获取 Doctrine 连接名称(如 ManagerRegistry
文件中配置)的方法:
use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
// ...
private function getConnectionName(ManagerRegistry $managerRegistry, Connection $targetConnection): string
{
foreach ($managerRegistry->getConnections() as $connectionName => $connection) {
if ($targetConnection === $connection) {
return $connectionName;
}
}
return '<unrecognized connection>';
}
// ...
试试这个
$conn = Doctrine_Manager::getInstance()->getConnection('doctrine')->getOptions();
$dns_array = split(';', $conn['dsn']);
preg_match('/host=(.*);/', $dns_array, $dbhost);
preg_match('/dbname=(.*)/', $dns_array, $dbname);
$dbname = $dbname;
$dbhost = $dbhost;
$dbuser = $conn['username'];
$dbpass = $conn['password'];
已更新
正如
Function split() is deprecated
所以这是推荐的方法。
$dns_array = explode(';', $conn['dsn']);
preg_match('/host=(.*)/', $dns_array[0], $dbhost);
preg_match('/dbname=(.*)/', $dns_array[2], $dbname);
$dbhost = $dbhost[1];
$dbname = $dbname[1];
$dbuser = $conn['username'];
$dbpass = $conn['password'];
截至 2024 年 1 月,这适用于 Symfony 7:
use Doctrine\ORM\EntityManagerInterface;
public function __construct(protected EntityManagerInterface $em)
{ }
$this->em->getConnection()->getParams();
它返回一个包含所有内容的标准数组(用户名、密码、主机……)