Skip to content

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 1, 2025

Q A
Type bug
BC Break no
Fixed issues Fix #2789

Summary

Instead of guessing the type of the version field value in order to convert the value into a MongoDB compatible value, we can use the type that is configured for this field. Otherwise the detection of the field type from the value type fails for custom types.

@GromNaN GromNaN added this to the 2.12.1 milestone Sep 1, 2025
@GromNaN GromNaN added the Bug label Sep 1, 2025
@GromNaN GromNaN changed the base branch from 2.13.x to 2.12.x September 1, 2025 15:07
@@ -218,7 +218,7 @@ public function executeInserts(array $options = []): void
$this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
}

$data[$versionMapping['name']] = $type->convertPHPToDatabaseValue($nextVersion);
Copy link
Member Author

@GromNaN GromNaN Sep 1, 2025

Choose a reason for hiding this comment

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

This is a static method called on the instance. So it was possible to change the behavior for a custom type by overriding the method, but that's not how it's supposed to be used.
In fact, this method should be final.

Copy link
Member

Choose a reason for hiding this comment

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

Are you suggesting that the convertPHPToDatabaseValue() should be final? If so, I agree and we should probably track that with a separate ticket for the next major version.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's the idea. But a better approach would be to extract the static methods into a TypeRegistry, similar to DBAL. #2818

@GromNaN GromNaN requested a review from Copilot September 1, 2025 16:26
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where custom version field types were not being properly converted to database values. Instead of guessing the field type, the code now uses the configured type for the version field to ensure proper conversion.

  • Replace static Type::convertPHPToDatabaseValue() calls with instance $type->convertToDatabaseValue()
  • Add comprehensive test coverage for custom version field types
  • Ensure custom types implementing Versionable work correctly with version field operations

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php Updates version field conversion to use the configured type instead of static type guessing
tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2789Test.php Adds test case demonstrating custom version field type functionality with Binary storage

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

LGTM, but two small suggestions.

@@ -218,7 +218,7 @@ public function executeInserts(array $options = []): void
$this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
}

$data[$versionMapping['name']] = $type->convertPHPToDatabaseValue($nextVersion);
Copy link
Member

Choose a reason for hiding this comment

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

Are you suggesting that the convertPHPToDatabaseValue() should be final? If so, I agree and we should probably track that with a separate ticket for the next major version.

@GromNaN GromNaN merged commit 1c2a146 into doctrine:2.12.x Sep 2, 2025
21 of 39 checks passed
@GromNaN GromNaN deleted the gh-2789 branch September 2, 2025 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use #[Version] with custom type
3 participants