Add #[AsDatabaseType] attribute to register DBAL types as Symfony services#2221
Open
GromNaN wants to merge 4 commits intodoctrine:3.3.xfrom
Open
Add #[AsDatabaseType] attribute to register DBAL types as Symfony services#2221GromNaN wants to merge 4 commits intodoctrine:3.3.xfrom
#[AsDatabaseType] attribute to register DBAL types as Symfony services#2221GromNaN wants to merge 4 commits intodoctrine:3.3.xfrom
Conversation
Introduces the `#[AsDatabaseType]` PHP attribute and a `DatabaseTypePass` compiler pass that builds a per-connection `TypeRegistry` and wires it to both the DBAL `Configuration` and the ORM `Configuration` of each entity manager, so custom types are resolved without touching the global static registry. Types can be restricted to specific connections by repeating the attribute with a `connection` argument. Config-based types (`doctrine.dbal.types`) are included in the registry as inline definitions.
GromNaN
commented
Apr 3, 2026
#[AsDatabaseType] attribute to register DBAL types as Symfony services
Replace local path repositories with VCS repositories pointing to the GitHub forks that contain the required changes: - GromNaN/dbal branch type-registry-config (TypeRegistry in Configuration) - GromNaN/doctrine-orm branch type-registry-instance (instance-based type lookups)
When no name is provided, the FQCN (service id) is used as the type name, allowing #[Column(type: MyType::class)] without declaring an explicit name. This also works for YAML/PHP service configuration: any service tagged with doctrine.dbal.type without a type attribute falls back to its service id.
99af197 to
1db2468
Compare
Replace the eager array of type instances/definitions with a Symfony ServiceLocator, leveraging the new ServiceProviderInterface support in DBAL's TypeRegistry. Types are now instantiated on first use rather than at container build time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces the
#[AsDatabaseType]PHP attribute and aDatabaseTypePasscompiler pass to register Doctrine DBAL types as Symfony services with a per-connectionTypeRegistry, without touching the global static type registry.How it works
#[AsDatabaseType(name: 'money')]attribute tags the service withdoctrine.dbal.typeDatabaseTypePassbuilds aTypeRegistryper connection, populated with:doctrine.dbal.types) as inline definitionsTypeRegistryis wired to both the DBALConfigurationand the ORMConfigurationof each associated entity manager#[AsDatabaseType(name: 'money', connection: 'reporting')]Example
Dependencies
This PR depends on changes in DBAL and ORM to support per-connection
TypeRegistry:TypeRegistrytoConfigurationand use it in all internal type resolution dbal#7342Type::*calls with instance-basedTypeRegistrylookups orm#12421Demo
A working example using
#[AsDatabaseType]to store emails as base64 in the database is available at: https://github.com/GromNaN/symfony-demo/tree/doctrine-type-service