Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for configuring DBAL to use existing connection #335

Closed
Revisor opened this issue Oct 3, 2014 · 9 comments
Closed

Support for configuring DBAL to use existing connection #335

Revisor opened this issue Oct 3, 2014 · 9 comments

Comments

@Revisor
Copy link

Revisor commented Oct 3, 2014

Hi,
DBAL accepts a parameter 'pdo' which allows it to reuse an existing PDO connection:
https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#driver

The Doctrine Bundle however doesn't accept this parameter. Is there any other clean way to make DBAL reuse an existing connection?

Thanks.

@pounard
Copy link

pounard commented Jul 20, 2016

+1 for this, I do need it too.

@jkoudys
Copy link

jkoudys commented Sep 1, 2016

+1 especially useful for those of us who want to start migrating legacy systems, and need to use some other DBAL (e.g. an old WordPress connection) and would prefer not to double the DB connections each request uses.

@kimhemsoe
Copy link
Member

I think you can do it with driver class.
Just create your own driver by copy it from dbal and you should be sorted i
believe.

When you have done the migration you can fallback to default drivers.

On Thu, Sep 1, 2016 at 2:44 AM, Joshua Koudys notifications@github.com
wrote:

+1 especially useful for those of us who want to start migrating legacy
systems, and need to use some other DBAL (e.g. an old WordPress connection)
and would prefer not to double the DB connections each request uses.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#335 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAit03q-eZBr7MQh85wYvWDUTdocbwFmks5qlh_YgaJpZM4Cqd5K
.

@ekonoval
Copy link

Does anybody managed to reuse the mysqli existing connection?

@kimhemsoe
Copy link
Member

You can get the mysqli connection handle from dbal if you wish. It is not pretty ofc.
Look at: MysqliConnection::getWrappedResourceHandle()
Dont think to much about the naming here :D

Alternative you can write you own driver, as a previous post mentions the easiest way to achieve that is by c/p the existing driver and make your changes, please don't try to extend them, just c/p

@ekonoval
Copy link

Thank you very much for detailed explanation, I'll try that =)

@ekonoval
Copy link

Finally I came across a solution like this:

$entityManager = EntityManager::create([
    //'driver'   => 'mysqli',
    'driverClass' => MysqliCustomDriver::class,

    'host' => DB_HOST,
    // ....
], $config);

class MysqliCustomDriver extends Driver
{
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
    {
//        return new MysqliCustomConnection($params, $username, $password, $driverOptions);
        return new MysqliCustomConnection([], '', '', []);
    }

    public function getName()
    {
        return 'mysqli_ib';
    }

}

class MysqliCustomConnection extends MysqliConnection {
    // a lot of copy-paste here ...

    public function __construct(array $params, $username, $password, array $driverOptions = [])
    {
        /**
         * \DB is my legacy class using mysqli and having already opened mysqli connection
         * which I'm going to reuse
         */
        $this->_conn = \DB::getInstance()->getConnectionID();
    }

    // a lot of copy-paste here ...
}

And I can even mix code from my legacy DB::getInstance()->query($sql) with DQL code in a single transaction and commit or roolback the set of mixed queries

@kimhemsoe
Copy link
Member

kimhemsoe commented Mar 21, 2018

Please dont extend MysqliConnection and etc, C/P into your own project

@ostrolucky ostrolucky changed the title How to make DBAL reuse an existing connection? Support for configuring DBAL to use existing connection Dec 1, 2019
@ostrolucky ostrolucky self-assigned this Jan 4, 2020
@ostrolucky
Copy link
Member

ostrolucky commented Apr 2, 2021

Sorry folks not happenning, DBAL is removing this option doctrine/dbal#3554.

@ostrolucky ostrolucky removed their assignment Apr 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants