DDC-2218: Unable to set custom PDO instance #2792

Closed
opened 2026-01-22 14:03:48 +01:00 by admin · 10 comments
Owner

Originally created by @doctrinebot on GitHub (Dec 31, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user mdobak:

Hi,

It's impossible to set custom instance of PDO using "pdo" parameter in connection params. Its cause errors like this:

Argument 1 passed to Doctrine\DBAL\Cache\ResultCacheStatement::**construct() must be an instance of Doctrine\DBAL\Driver\Statement, instance of PDOStatement given

Change "Statement $stmt" to just "$stmt" in Doctrine\DBAL\Cache\ResultCacheStatement constructor parameter list sloved problem.

Kind regards,
Michał Dobaczewski

Originally created by @doctrinebot on GitHub (Dec 31, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user mdobak: Hi, It's impossible to set custom instance of PDO using "pdo" parameter in connection params. Its cause errors like this: Argument 1 passed to Doctrine\DBAL\Cache\ResultCacheStatement::**construct() must be an instance of Doctrine\DBAL\Driver\Statement, instance of PDOStatement given Change "Statement $stmt" to just "$stmt" in Doctrine\DBAL\Cache\ResultCacheStatement constructor parameter list sloved problem. Kind regards, Michał Dobaczewski
admin added the Bug label 2026-01-22 14:03:48 +01:00
admin closed this issue 2026-01-22 14:03:48 +01:00
Author
Owner

@doctrinebot commented on GitHub (Dec 31, 2012):

Comment created by @ocramius:

Shouldn't you build the driver with your PDO instance?

@doctrinebot commented on GitHub (Dec 31, 2012): Comment created by @ocramius: Shouldn't you build the driver with your PDO instance?
Author
Owner

@doctrinebot commented on GitHub (Jan 1, 2013):

Comment created by mdobak:

Maybe buld custom driver will be better solution but as long this option is in documentation it should work or it should be deleted from documentation.

@doctrinebot commented on GitHub (Jan 1, 2013): Comment created by mdobak: Maybe buld custom driver will be better solution but as long this option is [in documentation](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html) it should work or it should be deleted from documentation.
Author
Owner

@doctrinebot commented on GitHub (Jan 1, 2013):

Comment created by @ocramius:

Could you expose your current config?

@doctrinebot commented on GitHub (Jan 1, 2013): Comment created by @ocramius: Could you expose your current config?
Author
Owner

@doctrinebot commented on GitHub (Jan 1, 2013):

Comment created by mdobak:

I have simple configuration without anything unusual based on code from this page:

[...]

$pdoInstance = new PDO('mysql:host=localhost;dbname=somedb', 'root', 'mypassword');

[...]

$connection['driver'] = 'pdo_mysql';
$connection['pdo']    = $pdoInstance; 

[...]

$config = Setup::createConfiguration();

[ Setup annotation driver, cache setup etc. ]

$entityManager = EntityManager::create($connection, $config,  $evm);

And now every Doctrine action which use database cause errors. If write this configuration:

$connection['driver']   = 'pdo_mysql';
$connection['user']     = 'root';
$connection['password'] = 'mypassword';

everything works.

@doctrinebot commented on GitHub (Jan 1, 2013): Comment created by mdobak: I have simple configuration without anything unusual based on code from [this page](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html): ``` [...] $pdoInstance = new PDO('mysql:host=localhost;dbname=somedb', 'root', 'mypassword'); [...] $connection['driver'] = 'pdo_mysql'; $connection['pdo'] = $pdoInstance; [...] $config = Setup::createConfiguration(); [ Setup annotation driver, cache setup etc. ] $entityManager = EntityManager::create($connection, $config, $evm); ``` And now every Doctrine action which use database cause errors. If write this configuration: ``` $connection['driver'] = 'pdo_mysql'; $connection['user'] = 'root'; $connection['password'] = 'mypassword'; ``` everything works.
Author
Owner

@doctrinebot commented on GitHub (Jan 1, 2013):

Comment created by @ocramius:

Weird... Just went through the code and everything looks fine (See Doctrine\DBAL\DriverManager#getConnection())

Can you check the spl_object_hash of your PDO instance and the one in the DBAL driver? What's your Connection#getDriver()?

@doctrinebot commented on GitHub (Jan 1, 2013): Comment created by @ocramius: Weird... Just went through the code and everything looks fine (See `Doctrine\DBAL\DriverManager#getConnection()`) Can you check the spl_object_hash of your PDO instance and the one in the DBAL driver? What's your `Connection#getDriver()`?
Author
Owner

@doctrinebot commented on GitHub (Jan 2, 2013):

Comment created by mdobak:

I checked the code again and I discovered, that happens If you use Doctrine\ORM\Query\Exec\SingleSelectExecutor with cache but is still a bug. Sorry to confuse you.

@doctrinebot commented on GitHub (Jan 2, 2013): Comment created by mdobak: I checked the code again and I discovered, that happens If you use Doctrine\ORM\Query\Exec\SingleSelectExecutor with cache but is still a bug. Sorry to confuse you.
Author
Owner

@doctrinebot commented on GitHub (Jan 2, 2013):

Comment created by @ocramius:

[~mdobak] can you provide the code to reproduce this then?

@doctrinebot commented on GitHub (Jan 2, 2013): Comment created by @ocramius: [~mdobak] can you provide the code to reproduce this then?
Author
Owner

@doctrinebot commented on GitHub (Jan 2, 2013):

Comment created by mdobak:

<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

require *_DIR_* . '/Doctrine/ORM/Tools/Setup.php';
require *_DIR_* . '/Entities/Foo.php';

Doctrine\ORM\Tools\Setup::registerAutoloadPEAR();

$paths = array("Entities");
$isDevMode = false;

$pdoInstance = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'mypass');

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'pdo'      => $pdoInstance
);

$config = Setup::createConfiguration($isDevMode);
$driverImpl = $config->newDefaultAnnotationDriver('/Entities');
$config->setMetadataDriverImpl($driverImpl);

$em = EntityManager::create($dbParams, $config);

$query = $em->createQuery('SELECT e FROM Foo e');
$query->useResultCache(true);
$query->setResultCacheLifetime(3600);
$query->setResultCacheDriver($em->getConfiguration()->getQueryCacheImpl());

// Below line is important. It force query to use SQLWalker which use ResultCacheStatement.
$query->setHint($query::HINT*CUSTOM_TREE*WALKERS, array('\Doctrine\ORM\Tools\Pagination\CountWalker'));

$query->getResult();

Foo is whatever correct entity.

@doctrinebot commented on GitHub (Jan 2, 2013): Comment created by mdobak: ``` <?php use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; require *_DIR_* . '/Doctrine/ORM/Tools/Setup.php'; require *_DIR_* . '/Entities/Foo.php'; Doctrine\ORM\Tools\Setup::registerAutoloadPEAR(); $paths = array("Entities"); $isDevMode = false; $pdoInstance = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'mypass'); // the connection configuration $dbParams = array( 'driver' => 'pdo_mysql', 'pdo' => $pdoInstance ); $config = Setup::createConfiguration($isDevMode); $driverImpl = $config->newDefaultAnnotationDriver('/Entities'); $config->setMetadataDriverImpl($driverImpl); $em = EntityManager::create($dbParams, $config); $query = $em->createQuery('SELECT e FROM Foo e'); $query->useResultCache(true); $query->setResultCacheLifetime(3600); $query->setResultCacheDriver($em->getConfiguration()->getQueryCacheImpl()); // Below line is important. It force query to use SQLWalker which use ResultCacheStatement. $query->setHint($query::HINT*CUSTOM_TREE*WALKERS, array('\Doctrine\ORM\Tools\Pagination\CountWalker')); $query->getResult(); ``` Foo is whatever correct entity.
Author
Owner

@doctrinebot commented on GitHub (May 1, 2013):

Comment created by @beberlei:

You need to set the statement to get it working:

$pdo->setAttribute(PDO::ATTR*STATEMENT*CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array()));
@doctrinebot commented on GitHub (May 1, 2013): Comment created by @beberlei: You need to set the statement to get it working: ``` $pdo->setAttribute(PDO::ATTR*STATEMENT*CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array())); ```
Author
Owner

@doctrinebot commented on GitHub (May 1, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (May 1, 2013): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2792