-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Provide for existing type to be of same output type for ConvertToPHPValue #3291
Provide for existing type to be of same output type for ConvertToPHPValue #3291
Conversation
IMO this looks good: some performance overhead is involved, but I don't think it is relevant, and indeed does avoid double conversions. Still, I don't think this is the correct fix for the underlying issue: types should be well-defined in and out, and these sort of "already PHP type shouldn't be converted again" shouldn't happen multiple times. I think the patch is fixing a symptom, rather than the root cause of the issue. |
Users are currently using the DoctrineObject hydrator to hydrate values they are creating themselves such as $doctrineObject->hydrate([['simple_array field', 'element2']], $entity); which, if the hydrator adhered strictly to Types would read $doctrineObject->hydrate(['simple_array_field,element2'], $entity); but this is counterintuitive because users are not thinking in raw database output. This patch fixes this behavior. But the bigger issue is custom types. A custom type must use a Type object because the input and output values could be anything. In my case I created a |
Right, but this is |
I agree the considerations are increased. I chose this path for the hydrator because we were already handling the conversion in most cases and I corrected only the few which did not conform to the pattern the others already followed. Modifying these in |
Aye, and that is fine to some degree, just unsure if we should really do it here, since we're no longer talking about what comes fro.m the RDBMS, and we're actually being too lax on the user. Instead, types should probably be stricter and throw more (for example when something is already a That's my feedback btw - let's see what other DBAL maintainers think. |
a9e912f
to
3e271a5
Compare
I'm with @Ocramius on this one. I'd better see the code made stricter in 3.0 by not checking if the value is already of the desired type rather than add more complexity to the code and lose this opportunity. |
3e271a5
to
424c331
Compare
This is an opportunity with two directions:
|
It explicitly removes the responsibility of the API for which it wasn't designed. Instead of being used for conversion of the data retrieved from the DB, the types are used for conversion of data provided by a developer which doesn't look like a valid argument. If we want to let the developer know if the value is already of the expected type, there should be a separate method for that which hydrator would call before conversion if it's a valid case for it. |
I also see a separate class to handle this. But that new set of classes would mirror the existing Types and would be required in addition to any user custom Type. I approached this from creating a new custom type. There was not a method of the hydrator to properly extract the value from my custom class. So should I have to write a second custom class which only the hydrator uses to handle extracting from that custom type? My custom type and many of the existing types already handled the job I needed them to do which is hydration. So if a fleet of TypeSupport files is needed in order to properly fix the hydrator I would only do that if I cannot convince |
No, I meant a new method of |
I created a new PR with the idea of a new function to handle type conversions on Type objects. |
a8c77ff
to
b416090
Compare
32ebdd8
to
f94e345
Compare
f94e345
to
b7cef3f
Compare
My main problem with this PR is that it complicates Type stuff even further, making duplicated calls part of the API (by adding normalization). I'd prefer this:
|
Co-Authored-By: TomHAnderson <[email protected]>
I like this approach with the extra method. It still allows restricting ->convertToPhpValue() It's not a core responsibility of DBAL but from a type/hydrator perspective it has to be put somewhere. If not here you get a more complex solution requiring extra configuration by the user and/or the author of the custom type has to supply conversion classes for every framework (not that I'm familiar with anything other than Zend). I'm sure you can come up with a case that won't work. The user is free to pick any serialization format after all. But it certainly works in common cases like Enum and UUID.
Not really an issue as you can regex for an expected token in the serialization format. |
Approving because I'm having a hard time understanding in what cases specifically this will be useful and I don't want to prevent a merge. |
…/dbal into feature/type-based-hydrator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still 👎 from me here for the reasons stated above:
- complicates Type even further, instead of the overall aim to simplify/split it;
- rather than treating Types as single-pass converters, this now leverages them as multi-pass normalizers, which could/will lead to more duplicated calls.
Also this does not handle cases when input and output is of the same type. Take for example ReverseStringType
, how do you know whether the value has already been converted or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation violates the SRP since every single implementation of normalizeToPHPValue()
calls convertToPHPValue()
internally. The diff between the two would constitute the logic I tried to describe in #3291 (comment):
If we want to let the developer know if the value is already of the expected type, there should be a separate method for that which the hydrator would call before conversion if it's a valid case for it.
However, even that doesn't stand the use case @Majkl578 described above. Any non-primitive type would have this issue.
The description for this PR states:
In order to execute that goal I have designed this PR to where it is today. But the issues raised don't address the original goal; perhaps too many changes had to be made to properly support custom types. Do we need to roll back all the changes and begin by removing the type checking from |
I've started a thread to talk through all the reasons I created this PR one at a time. |
Summary
The DoctrineObject Hydrator of DoctrineModule should use the Type objects to convert values into and out of arrays. Currently only a mediocre filter is used for the most basic of DateTime types.
Some of the existing Types (DateTimeType, DateTime, others) code in
convertToPHPValue
allow types of the same output type to be input data. This is desired. This PR fixes the types which do not allow such input.Hydrators
It is a goal of this change to create hydrators which fully support custom types.
Currently this function exists as https://github.com/doctrine/DoctrineModule/blob/3e21660d9ddce4cc223ebc2062c9a2824181bd60/src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php#L531-L588 and does not support custom field types.