我可以在Laravel中集成自定义PDO包装器吗?

时间:2015-06-18 13:09:35

标签: php laravel pdo vertica

我的工作人员和我正在尝试使用带有Vertica数据库的Laravel开发Web应用程序。唯一的问题是,只要将bindValue或bindParam与此特定数据库一起使用,PHP就会因分段错误而崩溃。所以我编写了一个PDO包装器类,它将调用重定向到PHP_ODBC模块并且实际上有效。我现在想知道如果这样的话甚至可能将它集成到Laravel中。

2 个答案:

答案 0 :(得分:3)

好的,经过大量的反复试验,我的同事和我设法让事情得到了解决。最耗时的部分是构建包装器。假设你有这个,那么你需要做的就是将它集成到Laravel中(顺便说一下,这些步骤适用于Laravel 5.1)。另外,我的包装器名为PDOVertica,所以每当你看到这个术语时,你必须用它替换你自己的包装器的名称。

1)将您的包装文件复制到以下文件夹:

vendor/laravel/framework/src/Illuminate/Database/Connectors

2)接下来,您需要修改几个文件:

供应商\ laravel \框架\ SRC \照亮\数据库\ 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 = [])

供应商\ laravel \框架\ SRC \照亮\数据库\连接器\ 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);
}

供应商\ laravel \框架\ SRC \照亮\数据库\连接器\ 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;
}

供应商\ laravel \框架\ SRC \照亮\数据库\连接器\ 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数据库而不会崩溃所需的所有步骤。我希望这会有所帮助。

答案 1 :(得分:0)

或者您可以使用支持laravel的PDO连接器:

composer require mixartemev/dbal-vertica-driver