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

Fix user provided pdo connection #4590

Merged

Conversation

mdumoulin
Copy link
Contributor

Q A
Type bug
BC Break no
Fixed issues #4564

Summary

When user provide a PDO connection, we get the following fatal error :

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Doctrine\DBAL\Connection::ensureForwardCompatibilityStatement() must be an instance of Doctrine\DBAL\Driver\ResultStatement, instance of PDOStatement given, called in vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php on line 1313 and defined in vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:1370
Stack trace:
#0 vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(1313): Doctrine\DBAL\Connection->ensureForwardCompatibilityStatement(Object(PDOStatement))
#1 t.php(6): Doctrine\DBAL\Connection->executeQuery('SELECT 1;')
#2 {main}
  thrown in vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php on line 1370

It happen because the PDO connection is not wrapped with Driver/Connection Interface in that case. The proposed solution, wrap the PDOStatement with Driver\PDO\Statement.

How to reproduce

<?php                                                                                                                   
require_once('vendor/autoload.php');                                                                                    
                                                                                                                        
$conn = Doctrine\DBAL\DriverManager::getConnection(['pdo' => new PDO('sqlite::memory:'), 'driver' => 'pdo_sqlite']);    
$conn->executeQuery('SELECT 1;');

@mdumoulin mdumoulin force-pushed the fix_user_provided_pdo_connection branch from 3b18f9b to c4d985a Compare April 7, 2021 21:14
@morozov morozov linked an issue Apr 8, 2021 that may be closed by this pull request
@morozov morozov added this to the 2.13.1 milestone Apr 8, 2021
@morozov morozov merged commit 670b8c0 into doctrine:2.13.x Apr 8, 2021
@morozov
Copy link
Member

morozov commented Apr 8, 2021

Thanks, @mdumoulin!

@mdumoulin
Copy link
Contributor Author

mdumoulin commented Apr 16, 2021

@morozov I'm affraid that the fix here may have side effects. If the user has already wrapped the \PDOStatement, it will be overidden in the DriverManager.

<?php
require_once('vendor/autoload.php');

class MyPDOStatement extends \PDOStatement
{
    public function execute($input_parameters = null)
    {
        var_dump('Hello there');

        return parent::execute($input_parameters);
    }
}

$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [MyPDOStatement::class, []]);

$conn = Doctrine\DBAL\DriverManager::getConnection(['pdo' => $pdo, 'driver' => 'pdo_sqlite']);
$result = $conn->executeQuery('SELECT 1;');
var_dump($result->fetchFirstColumn());

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for user-supplied PDO-instance broken/inconsistent with 2.13
3 participants