Monday, March 6, 2017

Laravel 5.4 redefining a connection prepared method

Okay, so my colleague and I are trying to configure a custom service provider in Laravel 5.4 but we're having a bit of trouble. So far here's what we have:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Connection;

class VerticaServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('db.connector.pgsql', \App\Vertica\PostgresConnector::Class);

        Connection::resolverFor('pgsql',function($connection, $database, $prefix, $config){
            return new \App\Vertica\Connection($connection, $database, $prefix, $config);
        });
    }
}

<?php

namespace App\Vertica;

use App\Vertica\PDOVertica;

use Illuminate\Database\Connection as BaseConnection;

class Connection extends BaseConnection
{
    /**
     * Configure the PDO prepared statement.
     *
     * @param  \PDOStatement  $statement
     * @return \PDOStatement
     */
    protected function prepared(PDOVerticaStatement $statement)
    {
        $statement->setFetchMode($this->fetchMode);
        $this->event(new Events\StatementPrepared(
            $this, $statement
        ));
        return $statement;
    }
}

The problem comes with the "prepared" method in the connection class. It expects a "PDOStatement" as its first parameter... but I absolutely need to be able to use a PDOVerticaStatement, because Vertica doesn't work well with PDO, so I had to create a wrapper around the ODBC functions of PHP.

So, now I need to redefine the method, but doing so makes it incompatible with the base class so I get an error. Is there any way I can work around that?



via Osuwariboy

Advertisement