SQLSTATE [08006] [7]无法将主机名“dbname =”转换为地址

时间:2016-07-28 14:51:59

标签: sql postgresql symfony authentication silex

我正在使用Symfony / Silex SecurityServiceProvider登录网站上的用户。

http://silex.sensiolabs.org/doc/providers/security.html

我一步一步地遵循指南,但是当我提交登录表单时,我收到了PostgreSQL错误

SQLSTATE[08006] [7] could not translate host name "dbname=" to address

无论我为dbname设置什么,我仍然得到错误。即使使用远程服务器的正确IP。但是我的设置很好,因为我使用相同的类来连接和请求服务器,它始终有效。

只有在提交登录表单时才会出现错误!

我的登录类

<?php

namespace Model;

use Silex\Application;

abstract class Entity {

    private $app;
    private $host;
    private $base;
    private $port;
    private $user;
    private $pass;
    private $charset;

    public function __construct(Application $app)
    {
        $this->app = $app;

        $this->host    = $app['config']['database']['host'];
        $this->base    = $app['config']['database']['base'];
        $this->port    = $app['config']['database']['port'];
        $this->user    = $app['config']['database']['user'];
        $this->pass    = $app['config']['database']['pass'];
        $this->charset = $app['config']['database']['charset'];
    }

    protected function connectBDD()
    {
        $connect = new \PDO("pgsql:host=$this->host;dbname=$this->base", $this->user, $this->pass);
        $connect->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $connect->query("SET NAMES '$this->charset'");

        return $connect;
    }
}
}

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

您的配置值为空。 $app['config']['database']['host']为空,连接类似于$connect = new \PDO("pgsql:host=;dbname=", ...);
尝试转储配置以查看值

public function __construct(Application $app)
{
    dump($app['config']['database']);
    ...
}