Closed gam6itko closed 1 year ago
You can get PDO
instance using closure:
$pdo = (fn () => $this->getPDO())->call($driver);
But it doesn't guarantie that PDO connection hasn't got a timeout error. If you want to just get a PDO connection that mustn't depend on DBAL then use
$pdo = (fn () => $this->createPDO())->call($driver);
Some variants for me
```php
// creates new Pdo connectino
class PdoFactory
{
protected function __construct(
protected DriverConfig $config
) {}
/**
* Create instance of configured PDO class.
*/
public function createPDO(): PDO|PDOInterface
{
$connection = $this->config->connection;
if (!$connection instanceof PDOConnectionConfig) {
throw new \InvalidArgumentException(
'Could not establish PDO connection using non-PDO configuration'
);
}
return new PDO(
dsn: $connection->getDsn(),
username: $connection->getUsername(),
password: $connection->getPassword(),
options: $connection->getOptions(),
);
}
}
But it doesn't guarantee that PDO connection hasn't got a timeout error.
Many drivers support automatic recovery for such case. F.e. PDO::MYSQL_ATTR_INIT_COMMAND.
I have an idea!
Hello! Some legacy classes in my project required \PDO instance. At the moment I am not able to get it from
Database
instance.In doctrine we have this opportunity.
I need some similar method in cycle.