Skip to content
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

[DoctrineBridge][DoctrineExtractor] Fix indexBy with custom and some core types #35794

Conversation

fancyweb
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets #35542 and #35604
License MIT
Doc PR -

For #35604:
To guess the collection key type, the getPhpType() method is called. But it does not handle most objects and arrays core types. This is why an indexBy datetime does not work.

For #35542:
When the php type cannot be guessed, null is returned. In this case, we cannot pass a valid builtin type to PropertyInfo Type, so we should return null.

@@ -117,7 +117,9 @@ public function getTypes($class, $property, array $context = [])
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
}

$collectionKeyType = $this->getPhpType($typeOfField);
if (!$collectionKeyType = $this->getPhpType($typeOfField)) {
return null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Type() requires a built in type a its first argument. If we cannot guess it (for custom types), then we cannot extract the property types.

@@ -234,7 +243,22 @@ private function getPhpType($doctrineType)
return Type::BUILTIN_TYPE_RESOURCE;

case DBALType::OBJECT:
case DBALType::DATE:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPhpType now handle all Doctrine core types.

return Type::BUILTIN_TYPE_OBJECT;

case DBALType::TARRAY:
Copy link
Contributor Author

@fancyweb fancyweb Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Doctrine ArrayType uses serialize / unserialize, so I think the type cannot be safely guessed. I have to test it and deprecate it if that's the case. This is why the JSON Doctrine type is not supported by this extractor I guess (it uses json_encode / json_decode).

Copy link
Contributor Author

@fancyweb fancyweb Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realize "json" is the new name of "json_array", so "json_array" has the same problem IMO.


case DBALType::TARRAY:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
if (!$builtinType = $this->getPhpType($typeOfField)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to refactor a bit here to avoid duplicate code and to avoid to process 2 times the $typeOfField.

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Feb 20, 2020
@fabpot
Copy link
Member

fabpot commented Feb 21, 2020

Thank you @fancyweb.

@fabpot fabpot merged commit 643f34f into symfony:3.4 Feb 21, 2020
@fancyweb fancyweb deleted the doctrine-bridge-property-info-extractor-fix-index-by branch February 21, 2020 08:09
This was referenced Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants