mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Allow to create Custom Type with metadata accessible from convertToPHPValue #7576
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @nfacciolo on GitHub (Nov 19, 2025).
Feature Request
What
Allow Custom Types to access field mapping metadata (like
optionsor a newclassparameter) inconvertToPHPValue()andconvertToDatabaseValue().Example usage:
Why
Currently, creating Value Objects requires one Custom Type per class, even when the conversion logic is identical (string ↔ VO with
__toString()).The
enumTypeparameter solves this elegantly for enums, but there's no equivalent for custom objects.This leads to:
A generic approach would allow a single
StringValueObjectTypeto handle all string-based Value Objects, similar to howenumTypeworks.How
Pass field mapping metadata to Type methods:
Alternatively, introduce a dedicated parameter like
valueObjectClasssimilar toenumType.This would maintain backward compatibility while enabling generic Type implementations for Value Objects.
@derrabus commented on GitHub (Nov 23, 2025):
Not sure if the DBAL type system is a good fit for this feature. Maybe we should implement this in the ORM instead, like we did with
enumType.@nfacciolo commented on GitHub (Nov 24, 2025):
Thank you for the feedback! I understand your point about the DBAL type system potentially not being the right fit.
I mentioned enumType precisely because I see it as a precedent for this kind of flexibility - if implementing this at the ORM level (similar to enumType) would be the better architectural approach, I'm completely open to that solution.
My core need is straightforward: I want to avoid creating multiple Type classes with identical conversion logic, differing only in the target Value Object class. Whether this is achieved through:
Field mapping metadata passed to DBAL Type methods, or
An ORM-level implementation like enumType
...either approach would solve my use case.
If the ORM layer is indeed the more appropriate place for this feature, I'd be happy to have the issue transferred there or to open a new one in the ORM repository. Just let me know what works best for the project architecture.
@derrabus commented on GitHub (Nov 29, 2025):
Done.
@beberlei commented on GitHub (Nov 30, 2025):
Why not use embeddebales?
@nfacciolo commented on GitHub (Nov 30, 2025):
Thanks for the suggestion! Embeddables are great for certain use cases, but they don't fit well here for a few reasons:
With embeddables, the Doctrine attributes (#[Column]) must be defined inside the Value Object class itself:
This creates a major problem: sometimes email must be unique, sometimes not, depending on the entity context. With embeddables, you're forced to choose one behavior for all cases, or create multiple Value Object classes for the same logical type.
With a custom Type, each entity controls its own constraints:
With a custom Type, the mapping is simpler and the same for each vo:
Value Objects are domain concepts that shouldn't know about database concerns. With embeddables, you're forced to pollute your Value Objects with Doctrine-specific attributes:
This creates tight coupling between the domain layer and the persistence layer.
With custom Types, the Value Object stays clean:
I have dozens of single-value Value Objects that each need:
Currently, I must create one Type class per Value Object, duplicating the same conversion code. I'm looking for a way to have one reusable Type that can handle multiple Value Object classes based on field metadata - similar to how enumType works.
Embeddables solve a different problem (multi-property value objects), not the "avoid duplicating Type classes" problem.