-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Query result type inference too generic for certain function results #272
Comments
/cc @arnaud-lb |
Hi @janedbal Thank you for your detailed bug reports :) Doctrine does a good job at consistently returning the right type for entity fields. However, for expressions or function calls it just returns the raw value from the database. So the type of function calls and arithmetic expressions really depends on the database driver (and the driver version). For instance, the pdo_mysql and pdo_sqlite drivers return integers and floats as strings in PHP < 8.1, or as integer/float in PHP 8.1 (unless PDO::ATTR_STRINGIFY_FETCHES is set, and unless the integer/float is too large to fit in PHP's integer/float types). Since Doctrine 2.8, the functions COUNT and LENGTH are casted as int by Doctrine, though, so phpstan-doctrine infers these expressions as just For other expressions, I chose to infer types that are correct in all environments, for now. It may be possible to refine these in a future version, although it may be challenging in some cases, and also lead to type instability (it depends on too many factors that may change depending on the environment). In short, depending on many factors including the PHP version, drivers, and driver options, integer expressions in DQL queries may be returned as
|
Thank you, now I see the huge complexity you were dealing when implementing this. Good work! Anyway, shouldnt you be able to detect all those settings from EM when provided (parameters.doctrine.objectManagerLoader)? Driver should be known, PHP version too, Doctrine version as well. Problematic might be PDO setup (if you dont want to query the database). |
We could definitely, but there is the question of the type stability: It can actually be valuable that inferred types don't change with the environment, as it makes the resulting code more portable. Maybe we could add a config parameter for this. |
I think that would be beneficial. |
That might be super important for libraries, but not for company codebase, where you use single driver and single setup. So as said, some config might be handy so that everybody could decide what is better for his codebase. |
Can I bump this up? return $this->createQueryBuilder('pt')
->select(['pt as postTag', 'count(p) as cnt'])
//...
->getResult(); Doctrine 3, Postgres 15. |
I'm currently working on precise implementation of types coming from such DQLs: #506 So you can expect this to be correct in near future. |
I can't understand why it's been broken from 1.4, but OK, let's wait for improve. Thanks! |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
int<0, max>|numeric-string
.The text was updated successfully, but these errors were encountered: