无法使用雄辩的胶囊管理器捕获PDO异常

时间:2019-04-29 13:22:43

标签: php exception eloquent

我有一堂课,扩展了雄辩的胶囊管理器:

class Database extends \Illuminate\Database\Capsule\Manager

在其中,我要在构造函数中添加一个连接:

public function __construct()
{ 
    parent::__construct();

    try {
        $this->addConnection([
            'driver' => 'mysql',
            'host' => $server,
            'database' => $database,
            'username' => $username,
            'password' => $password,
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
        ]);

        $this->setAsGlobal();
        $this->bootEloquent();

    } catch (\Exception $ex) {
        exit("Can't connect to database");
    }
}

这里的问题是从不调用catch,如果任何数据库连接信息为假,我将得到一个Fatal error: Uncaught PDOException,而不是我想要获得的出口。

我认为问题是由于我有我自己的类,该类扩展了胶囊管理器,但是我如何使用雄辩的语言来捕获我自己类中的连接错误?

编辑:回溯-

  

致命错误:未捕获PDOException:PDO :: __ construct():php_network_getaddresses:getaddrinfo失败:在/var/www/vendor/illuminate/database/Connectors/Connector.php中未知的名称或服务:70堆栈跟踪:#0 /var/www/vendor/illuminate/database/Connectors/Connector.php(70):PDO-> __ construct('mysql:host = mari ...','root','root',Array)#1 / var /www/vendor/illuminate/database/Connectors/Connector.php(46):Illuminate \ Database \ Connectors \ Connector-> createPdoConnection('mysql:host = mari ...','root','root',Array) #2 /var/www/vendor/illuminate/database/Connectors/MySqlConnector.php(24):Illuminate \ Database \ Connectors \ Connector-> createConnection('mysql:host = mari ...',Array,Array)#3 /var/www/vendor/illuminate/database/Connectors/ConnectionFactory.php(182):Illuminate\Database\Connectors\MySqlConnector->connect(Array)#4 [内部功能]:Illuminate \ Database \ Connectors \ ConnectionFactory-> Illuminate \ Database \ Connectors {closure}()#5 /var/www/vendor/illuminate/database/Connection.php(918 ):第664行的/var/www/vendor/illuminate/database/Connection.php中的call_user_func(Object(Closure))#6 / va

使类看起来像这样,并扩展selectupdateinsert函数:

class Database extends \Illuminate\Database\Capsule\Manager
{

    public function __construct()
    {
    }

    public static function select(string $query, array $params = [])
    {

    }

    public static function insert(string $query, array $params = [])
    {
    }

    public static function update(string $query, array $params = [])
    {
        return parent::update($query, $params);
    }

}

然后编写例如:

new Database;
Database::update('XXX', []);

0 个答案:

没有答案