-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Insufficient autodetection of query parameter type #6443
Comments
Sorry the question, but should this fix your issue?
|
Actually that was a mistake. The default timezone of our system can be different than UTC which is when the bug occurs. |
Inferring types is actually very risky, so I'd stop going down that way. Assuming you have a dozen of different types that map to A good alternative could be to enforce the type parameter to be given at all times (BC break) |
@Ocramius are you suggesting a sort of ->set[typeA|typeB|...]Parameter()? |
@sensorario no, |
Setting parameter type in every use of But we also use own type and I was suprised that I have to set it's type by hand. We use own type for our own class, so there is no issue with multiple types for the same class, like in @enumag case. Wouldn't be better if in |
That would mean Also, the order of registered types would be authoritative. IMO, that is both a performance and a security nightmare. |
Well, not necessary Authoritative order - no, if we assume that build-in types have priority (you have to set type directly as it is now to override it) and then if more then one type maps the same class - throw exception. It will work without providing type by hand in most cases. Security - well, I will not speak in this matter. If you say so, ok. But with priority of build-in Doctrine types our new "magic" code will work only when you put custom object as parameter, so you should be aware of Type detection. And with exception if more then one Type is given for class noone will override your mapping I think. But again, there may be more on this subject so I'm leaving it under your consideration. |
It's a slippery slope IMO, as the opinionated approach of |
I do agree with @enumag , When you're working on a multi-international project it's very important to store all the dates in UTC, doctrine handles this well by defining a new type, but it would make no sense that it doesn't use the same type when querying the same Entity. |
…e parameters (#8328) The support for passing \DateTimeImmutable instance as a query parameter has been added to ORM in #1333 (the year 2015), a long time before immutable date types (datetime_immutable etc) were introduced to DBAL in doctrine/dbal#2450 (2017). Back then, it made sense to treat \DateTimeImmutable (or any \DateTimeInterface) in the same way as \DateTime and infer parameter type as datetime. However, when immutable date types were later added to DBAL, it wasn't reflected anyhow in type inference in ORM and \DateTimeImmmutable instances are still inferred as datetime DBAL type. This PR fixes this IMO incorrect behaviour of ParameterTypeInferer::inferType(): for a \DateTimeImmmutable parameter, it now returns datetime_immutable DBAL type; for \DateTime or any other types implementing \DateTimeInterface, it returns datetime DBAL type as it did before. This behaviour is in line with DateTimeImmutableType handling only \DateTimeImmutable and DateTimeType handling any \DateTimeInterface. Why? In most cases, it doesn't matter and datetime works for \DateTimeImmutable parameters just fine. But it does matter if using custom implementation of datetime_immutable type like UTCDateTimeImmutableType from simpod/doctrine-utcdatetime. Then the broken type inference is revealed. This is partially related to #6443, however, this PR isn't about custom DBAL types but about correct type inference for build-in types.
We're using a custom UtcDateTimeType. Recently we found a bug in our application that queries with a condition using such column do not return correct results.
It can be easily fixed by specifying the parameter type in the query:
I don't like this solution because programmer can easily forget about this or other members of the team may not be aware of the issue. So I looked into Doctrine internals to check why the type is not autodetected from the metadata as I would expect.
The problem is in Query::processParameterMappings(). Instead of getting the parameter type from metadata the type is guessed by static ParameterTypeInferer which of course only considers built-in Doctrine types.
Is it possible to improve Doctrine to guess the parameter type correctly based on which column is the parameter used for? If not can you make the ParameterTypeInferer non-static and replaceable?
The text was updated successfully, but these errors were encountered: