[DDC-2218] Unable to set custom PDO instance Created: 31/Dec/12  Updated: 01/May/13  Resolved: 01/May/13

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: None
Affects Version/s: 2.3.1
Fix Version/s: None
Security Level: All

Type: Bug Priority: Major
Reporter: Michał Dobaczewski Assignee: Benjamin Eberlei
Resolution: Invalid Votes: 0
Labels: None


 Description   

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



 Comments   
Comment by Marco Pivetta [ 31/Dec/12 ]

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

Comment by Michał Dobaczewski [ 01/Jan/13 ]

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.

Comment by Marco Pivetta [ 01/Jan/13 ]

Could you expose your current config?

Comment by Michał Dobaczewski [ 01/Jan/13 ]

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.

Comment by Marco Pivetta [ 01/Jan/13 ]

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()`?

Comment by Michał Dobaczewski [ 02/Jan/13 ]

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.

Comment by Marco Pivetta [ 02/Jan/13 ]

Michał Dobaczewski can you provide the code to reproduce this then?

Comment by Michał Dobaczewski [ 02/Jan/13 ]
<?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.

Comment by Benjamin Eberlei [ 01/May/13 ]

You need to set the statement to get it working:

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Doctrine\DBAL\Driver\PDOStatement', array()));
Generated at Fri Aug 22 03:57:09 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.