Home > Software engineering >  How to get php-native handle from Doctrine\DBAL\Connection?
How to get php-native handle from Doctrine\DBAL\Connection?

Time:10-22

Given the code

$connectionParams = array(
    'dbname' => $this->dbname,
    'user' => $this->dbuser,
    'password' => $this->dbpass,
    'host' => $this->dbhost,
    'driver' => 'mysqli',
);

$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);        
var_dump($conn);

How can I get the underlying mysqli handle from $conn (which is a Doctrine\DBAL\Connection)?

I have found *a way* to access it, but its obviously not the way it's supposed to be done, so I'm up for suggestions:

$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);        
foreach((array)$conn->getWrappedConnection() as $mysqli){
    // TODO: find official way of getting the handle.
    // here we are casting it to (array) to access its PRIVATE PROPERTIES
    // it's a fugly hack. 
    break;
}
var_dump($mysqli);

CodePudding user response:

You can get it this way:

$mysqli = $conn->getWrappedConnection()->getWrappedResourceHandle();
  • Related