doctrine / dbal

Doctrine Database Abstraction Layer
https://www.doctrine-project.org/projects/dbal.html
MIT License
9.4k stars 1.33k forks source link

Unable to detect real driver for proper query type inference in phpstan-doctrine #6440

Closed janedbal closed 3 weeks ago

janedbal commented 3 weeks ago

Support Question

Hi, I'm trying to improve query type inference support in phpstan-doctrine, see PR. Any type of DQL expression is dependent on used driver (and PHP version and config). I tried to make this work out of the box (by autodetection) and so far I used this for conditional logic:

$em->getConnection()->getDriver() instanceof Doctrine\DBAL\Driver\PgSQL\Driver // not working with middlewares

But now I realized that when middlewares are used, this will never match. And I found no reliable way in public api of doctrine/dbal to detect actually used driver. Did I miss some approach or do you think it would be handy even for other usecases to somehow expose really used driver?

I dont care about the instance itself, I could work even with some enum like those used in connection params or just its class-string.

derrabus commented 3 weeks ago

getNativeConnection() is the best the public API can offer at runtime. Also note that middleware could perform arbitrary transformations on results, like type casting or changing array keys.

janedbal commented 3 weeks ago

Ok, I ended up using that anyway. Thanks for confirmation.