您可以尝试实现和使用您自己的 driver_class 并在 PDO DriverOptions 中传递 search_path,例如在您的 symfony 配置中:
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_pgsql
driver_class: YourNamespace\YourBundle\Doctrine\DBAL\Driver\PDOPgSql\Driver
options:
search_path: YOUR_SEARCH_PATH
驱动程序可能看起来像这样:
namespace YourNamespace\YourBundle\Doctrine\DBAL\Driver\PDOPgSql;
use Doctrine\DBAL\Platforms;
class Driver extends \Doctrine\DBAL\Driver\PDOPgSql\Driver implements \Doctrine\DBAL\Driver
{
public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
{
// ADD SOME ERROR HANDLING WHEN THE SEARCH_PATH IS MISSING...
$searchPath = $driverOptions['search_path'];
unset($driverOptions['search_path']);
$connection = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params),
$username,
$password,
$driverOptions
);
$connection->exec("SET SEARCH_PATH TO {$searchPath};");
return $connection;
}
/**
* Constructs the Postgres PDO DSN.
*
* @return string The DSN.
*/
protected function _constructPdoDsn(array $params)
{
$dsn = 'pgsql:';
if (isset($params['host']) && $params['host'] != '') {
$dsn .= 'host=' . $params['host'] . ' ';
}
if (isset($params['port']) && $params['port'] != '') {
$dsn .= 'port=' . $params['port'] . ' ';
}
if (isset($params['dbname'])) {
$dsn .= 'dbname=' . $params['dbname'] . ' ';
}
return $dsn;
}
}
您需要 _constructPdoDsn 方法,因为它没有在 \Doctrine\DBAL\Driver\PDOPgSql\Driver 中定义为受保护的。这有点“hacky”,因为我们正在使用 PDO DriverOptions,我不确定这是否是一个好方法 - 但它似乎有效。
希望这可以帮助。
最好的祝福,
帕特里克