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

Add TypedFieldMapper for automatic mapping of typed PHP fields to DBAL types #10313

Merged
merged 1 commit into from Dec 18, 2022

Conversation

michnovka
Copy link
Contributor

@michnovka michnovka commented Dec 16, 2022

This is an alternative implementation of #10290, fixes #9561

Previously, only a predefined set of automatic mappings was allowed for as array, bool, int, float, string, DateTime, DateTimeImmutable and DateInterval.

With this extension, it is possible to supply custom TypedFieldMapper implementation. Its validateAndComplete takes as parameter the ReflectionProperty of a given field and decides the appropriate mapping.

A new configuration option was added to set and get the TypedFieldMapper.

The old logic was moved into a class DefaultTypedFieldMapper which is used by default when no mapper is supplied.

The selected TypedFieldMapper is passed into ClassMetadataInfo's constructor. If empty, the DefaultTypedFieldMapper is used.

There is also ChainTypedFieldMapper class which allows chaining multiple TypedFieldMappers and apply them in a cascade (always if a field gets type assigned by the earlier mapper in the list, it will not be changed later).

Documentation has been updated to explain the new features. Tests were added to cover the new classes as well as ClassMetadataInfo and all mapping drivers.

@michnovka
Copy link
Contributor Author

michnovka commented Dec 16, 2022

@stof Please have a look and let me know your opinion. @beberlei @greg0ire and @derrabus also welcome your feedback

greg0ire
greg0ire previously approved these changes Dec 16, 2022
docs/en/reference/typedfieldmapper.rst Outdated Show resolved Hide resolved
docs/en/reference/typedfieldmapper.rst Outdated Show resolved Hide resolved
lib/Doctrine/ORM/Configuration.php Outdated Show resolved Hide resolved
tests/Doctrine/Tests/ORM/Mapping/TypedFieldMapperTest.php Outdated Show resolved Hide resolved
lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php Outdated Show resolved Hide resolved
lib/Doctrine/ORM/Mapping/DefaultTypedFieldMapper.php Outdated Show resolved Hide resolved
lib/Doctrine/ORM/Mapping/DefaultTypedFieldMapper.php Outdated Show resolved Hide resolved
tests/Doctrine/Tests/ORM/Mapping/MappingDriverTestCase.php Outdated Show resolved Hide resolved
…L types

Previously, only a predefined set of automatic mappings was allowed, such as int, float, boolean, DateTime etc.
With this extension, it is possible to supply custom TypedFieldMapper implementation which takes as parameter the ReflectionProperty of a given field and decides the appropriate mapping.
A new configuration option was added to set and get the TypedFieldMapper.
The old logic was moved into a class DefaultTypedFieldMapper which is used by default when no mapper is supplied.
The selected TypedFieldMapper is passed into ClassMetadataInfo constructor. If empty, the DefaultTypedFieldMapper is used.
There is also ChainTypedFieldMapper class which allows chaining multiple TypedFieldMappers and apply them in a cascade (always if a field gets type assigned by the earlier mapper in the list, it will not be changed later).
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

LGTM

@derrabus derrabus added this to the 2.14.0 milestone Dec 18, 2022
@derrabus derrabus merged commit 6255461 into doctrine:2.14.x Dec 18, 2022
derrabus added a commit to derrabus/orm that referenced this pull request Dec 19, 2022
* 2.14.x:
  Drop forceful loading of annotations (doctrine#10321)
  Document stdClass structures used by CommitOrderCalculator (doctrine#10315)
  Psalm 5.3.0 (doctrine#10317)
  PHPStan 1.9.4 (doctrine#10318)
  add apcu enabled check if apcu extension loaded (doctrine#10310) (doctrine#10311)
  Add TypedFieldMapper for automatic mapping of typed PHP fields to DBAL types (doctrine#10313)
derrabus added a commit to derrabus/orm that referenced this pull request Dec 19, 2022
* 2.14.x:
  Drop forceful loading of annotations (doctrine#10321)
  Document stdClass structures used by CommitOrderCalculator (doctrine#10315)
  Psalm 5.3.0 (doctrine#10317)
  PHPStan 1.9.4 (doctrine#10318)
  add apcu enabled check if apcu extension loaded (doctrine#10310) (doctrine#10311)
  Add TypedFieldMapper for automatic mapping of typed PHP fields to DBAL types (doctrine#10313)
n-e-m-a-nj-a pushed a commit to n-e-m-a-nj-a/orm that referenced this pull request Jan 27, 2023
…L types (doctrine#10313)

Previously, only a predefined set of automatic mappings was allowed, such as int, float, boolean, DateTime etc.
With this extension, it is possible to supply custom TypedFieldMapper implementation which takes as parameter the ReflectionProperty of a given field and decides the appropriate mapping.
A new configuration option was added to set and get the TypedFieldMapper.
The old logic was moved into a class DefaultTypedFieldMapper which is used by default when no mapper is supplied.
The selected TypedFieldMapper is passed into ClassMetadataInfo constructor. If empty, the DefaultTypedFieldMapper is used.
There is also ChainTypedFieldMapper class which allows chaining multiple TypedFieldMappers and apply them in a cascade (always if a field gets type assigned by the earlier mapper in the list, it will not be changed later).
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

5 participants