Skip to content

Add #[AsDatabaseType] attribute to register DBAL types as Symfony services#2221

Open
GromNaN wants to merge 4 commits intodoctrine:3.3.xfrom
GromNaN:feat/dbal-type-as-service
Open

Add #[AsDatabaseType] attribute to register DBAL types as Symfony services#2221
GromNaN wants to merge 4 commits intodoctrine:3.3.xfrom
GromNaN:feat/dbal-type-as-service

Conversation

@GromNaN
Copy link
Copy Markdown
Member

@GromNaN GromNaN commented Apr 3, 2026

Introduces the #[AsDatabaseType] PHP attribute and a DatabaseTypePass compiler pass to register Doctrine DBAL types as Symfony services with a per-connection TypeRegistry, without touching the global static type registry.

How it works

  • A new #[AsDatabaseType(name: 'money')] attribute tags the service with doctrine.dbal.type
  • DatabaseTypePass builds a TypeRegistry per connection, populated with:
    • Config-based types (doctrine.dbal.types) as inline definitions
    • Service-tagged types as service references
  • The TypeRegistry is wired to both the DBAL Configuration and the ORM Configuration of each associated entity manager
  • Types can be restricted to specific connections by repeating the attribute: #[AsDatabaseType(name: 'money', connection: 'reporting')]

Example

use Doctrine\Bundle\DoctrineBundle\Attribute\AsDatabaseType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

#[AsDatabaseType(name: MoneyType::NAME)]
final class MoneyType extends Type
{
    public const string NAME = 'money';

    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
    {
        return $platform->getDecimalTypeDeclarationSQL($column);
    }
}
#[ORM\Column(type: MoneyType::NAME)]
private Money $price;

Dependencies

This PR depends on changes in DBAL and ORM to support per-connection TypeRegistry:

Demo

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

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.
Comment thread src/Attribute/AsDatabaseType.php Outdated
@GromNaN GromNaN changed the title Add #[AsDatabaseType] attribute to register DBAL types as Symfony services Add #[AsDatabaseType] attribute to register DBAL types as Symfony services Apr 3, 2026
GromNaN added 2 commits April 3, 2026 15:22
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.
@GromNaN GromNaN force-pushed the feat/dbal-type-as-service branch from 99af197 to 1db2468 Compare April 3, 2026 13:42
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants