我和我的同事正在尝试使用 Laravel 和 Vertica 数据库开发一个 Web 应用程序。唯一的问题是,一旦您对这个特定的数据库使用bindValue 或bindParam,PHP 就会因分段错误而崩溃。因此,我编写了一个 PDO 包装类,它将调用重定向到 PHP_ODBC 模块,并且确实有效。我现在想知道如何将它集成到 Laravel 中,如果这样的事情可能的话。
好吧,经过大量的尝试和错误,我和我的同事们设法让一切正常运行。最耗时的部分是构建包装器。假设你已经有了,那么将其集成到 Laravel 中需要执行以下操作(顺便说一下,这些步骤适用于 Laravel 5.1)。另外,我的包装器名为 PDOVertica,因此每当您看到这个术语时,您都必须将其替换为您自己的包装器的名称。
1) 将包装文件复制到以下文件夹:
vendor/laravel/framework/src/Illuminate/Database/Connectors
2)接下来,您需要修改几个文件:
vendor\laravel ramework\src\Illuminate\Database\Connection.php
namespace Illuminate\Database;
use PDO;
use PDOVertica; //Add this line
use Closure;
use DateTime;
...
//Change the type of the first parameter to PDOVertica as follow
public function __construct(PDOVertica $pdo, $database = '', $tablePrefix = '', array $config = [])
vendor\laravel ramework\src\Illuminate\Database\Connectors\Connector.php
namespace Illuminate\Database\Connectors;
include 'clsPDOVertica.php'; //Add this line
use PDO;
use PDOVertica; //Add this line
...
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
//Modify the return value to return your wrapper
return new PDOVertica($dsn, $username, $password, $options);
}
vendor\laravel ramework\src\Illuminate\Database\Connectors\PostgresConnector.php
protected function getDsn(array $config)
{
extract($config);
$host = isset($host) ? "Server={$host};" : '';
// Modify this line so that it creates the Vertica DSN.
// It should look something like this.
$dsn = "Driver=/opt/vertica/lib64/libverticaodbc.so;{$host}Database={$database}";
if (isset($config['port'])) {
$dsn .= ";port={$port}";
}
if (isset($config['sslmode'])) {
$dsn .= ";sslmode={$sslmode}";
}
return $dsn;
}
vendor\laravel ramework\src\Illuminate\Database\Connectors\ConnectionFactory.php
namespace Illuminate\Database\Connectors;
use PDO;
use PDOVertica; //Add this line
use InvalidArgumentException;
...
// Modify the header of this function so that the $connection parameter
// is of type PDOVertica
protected function createConnection($driver, PDOVertica $connection, $database, $prefix = '', array $config = [])
{
if ($this->container->bound($key = "db.connection.{$driver}")) {
return $this->container->make($key, [$connection, $database, $prefix, $config]);
}
switch ($driver) {
case 'mysql':
return new MySqlConnection($connection, $database, $prefix, $config);
case 'pgsql':
return new PostgresConnection($connection, $database, $prefix, $config);
case 'sqlite':
return new SQLiteConnection($connection, $database, $prefix, $config);
case 'sqlsrv':
return new SqlServerConnection($connection, $database, $prefix, $config);
}
throw new InvalidArgumentException("Unsupported driver [$driver]");
}
3)正确修改文件后,您所要做的就是通过修改以下文件来正确配置 Laravel 以使用自定义连接:
配置/database.php
/* |--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => 'vertica',
...
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', ''),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'port' => '5433h',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
//This is our custom connection
'vertica' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '192.168.1.1'),
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'mypassword'),
'port' => '5433',
'charset' => 'utf8',
'schema' => 'myschema',
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'port' => '5433',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'prefix' => '',
],
],
据我所知,这就是让 Laravel 连接到 Vertica 数据库而不崩溃所需的所有步骤。我希望这有帮助。
您可以使用我编写的 Laravel-ready PDO 连接器:
composer require mixartemev/dbal-vertica-driver
@Osuwariboy 感谢我解决了平台特定要求的错误 https://neon.tech/ 将 BD postgress 连接到 Laravel:
错误:
PDOException::("SQLSTATE[08006] [7] 错误:未指定端点 ID。请升级 postgres 客户端库 (libpq) 以支持 SNI,或将端点 ID(域名的第一部分)作为参数传递: '?选项=项目%3D'
在我的例子中,看看你如何编写条件 $config['ssl模式'] 在文件中 vendor\laravel ramework\src\Illuminate\Database\Connectors\PostgresConnector.php 你帮我谢谢。
然后在我的情况下,我需要特定的参数“选项”以及 BD 平台所需的字符https://neon.tech/ 用于设置参数 '选项='项目='
示例:
protected function getDsn(array $config)
{
// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the PDO connections and return them back for use.
extract($config, EXTR_SKIP);
$host = isset($host) ? "host={$host};" : '';
$dsn = "pgsql:{$host}dbname='{$database}'";
// If a port was specified, we will add it to this Postgres DSN connections
// format. Once we have done that we are ready to return this connection
// string back out for usage, as this has been fully constructed here.
if (isset($config['port'])) {
$dsn .= ";port={$port}";
}
//$config['options_extra'] is a parameter custom in config/database.php with value true only for set this code with a custom flag
if (isset($config['options_extra'])) {
$dsn .= ";options='project=<endpoint-id>'";
}
return $this->addSslOptions($dsn, $config);
}
我的案例结论:
连接成功,如果有这个错误记得替换
<endpoint-id>
为你的数据