Skip to content

Add #[AsDbalType] attribute#2197

Merged
GromNaN merged 1 commit intodoctrine:3.3.xfrom
syl20b:add-as-doctrine-type-attribute
Apr 20, 2026
Merged

Add #[AsDbalType] attribute#2197
GromNaN merged 1 commit intodoctrine:3.3.xfrom
syl20b:add-as-doctrine-type-attribute

Conversation

@syl20b
Copy link
Copy Markdown

@syl20b syl20b commented Jan 30, 2026

Description

This PR add a new #[AsDbalType] attribute to automatically register the related class as a DBAL type.

Problem Solved

Currently, to register a custom DBAL type, it must be manually declared in the Doctrine configuration file:

# config/packages/doctrine.yaml

doctrine:
    dbal:
        types:
            my_custom_type: App\Type\MyCustomType

This approach is verbose and requires the developer to manage configuration in addition to the type class itself.

Feature Added

The addition of the #[AsDbalType] attribute allows the application to automatically detect and register any class that uses this attribute.


Concrete Usage Example

Here is how you can register a custom type directly via the attribute, without touching the YAML configuration:

1. Creating the Custom Type

<?php

namespace App\Type;

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

#[AsDbalType(name: 'my_custom_type')]
final class MyCustomType extends Type
{
    // ... implementation of methods
}

2. Usage in an Entity

You can then use this type directly in your entities:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

class Product
{
    #[ORM\Column(type: 'my_custom_type', length: 255)]
    private ?string $name = null;
    
    // ...
}

Advantages

  • Zero Configuration: No need to modify doctrine.yaml for every new custom type.
  • Discoverability: The DBAL type and its name are declared in the same location as its implementation.

Comment thread src/DependencyInjection/Compiler/RegisterAsDoctrineTypeAttributePass.php Outdated
Comment thread src/DependencyInjection/Compiler/RegisterAsDoctrineTypeAttributePass.php Outdated
Comment thread src/Attribute/AsDoctrineType.php Outdated
Comment thread src/DependencyInjection/Compiler/RegisterAsDoctrineTypeAttributePass.php Outdated
Comment thread src/DependencyInjection/Compiler/RegisterAsDoctrineTypeAttributePass.php Outdated
@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch 4 times, most recently from e26bfe1 to ea214eb Compare February 1, 2026 22:08
@syl20b syl20b changed the title feat: add AsDoctrineType attribute feat: add AsDbalType attribute Feb 1, 2026
@syl20b syl20b requested a review from stof February 12, 2026 08:28
Comment thread tests/DependencyInjection/DoctrineExtensionTest.php
@syl20b
Copy link
Copy Markdown
Author

syl20b commented Mar 11, 2026

@stof is it ok for you?

@silasjoisten
Copy link
Copy Markdown

I think this PR belongs more into the symfony/doctrine-bridge and there is already a PR for this: symfony/symfony#63774

@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 2, 2026

@silasjoisten

I think this PR belongs more into the symfony/doctrine-bridge and there is already a PR for this: symfony/symfony#63774

AFAIK, the bridge aims to do a bridge between a Symfony component and Doctrine, but this feature is not related to a Symfony component, so it should belongs to the bundle IMHO

@silasjoisten
Copy link
Copy Markdown

I’m not fully convinced this should live in the bundle.

According to the Symfony docs, DoctrineBridge is meant to “provide integration for Doctrine with Symfony components”, while DoctrineBundle mainly handles configuration and wiring.

This change feels more like integration logic (how Symfony registers and discovers Doctrine types) rather than something that should be driven by bundle configuration.

We already keep things like Doctrine types and Symfony specific behavior in the Bridge, so moving the registration logic there would keep responsibilities more clearly separated and consistent with existing patterns.

Otherwise we risk spreading behavior across both layers, which makes it harder to reason about and reuse outside of a full DoctrineBundle setup.

@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 2, 2026

@silasjoisten

According to the Symfony docs, DoctrineBridge is meant to “provide integration for Doctrine with Symfony components”, while DoctrineBundle mainly handles configuration and wiring.

I agree with that, but there is no component involved here

This change feels more like integration logic (how Symfony registers and discovers Doctrine types) rather than something that should be driven by bundle configuration.
We already keep things like Doctrine types and Symfony specific behavior in the Bridge, so moving the registration logic there would keep responsibilities more clearly separated and consistent with existing patterns.

Types you mention are Symfony component related types (Uid, Clock,...)

@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 2, 2026

@greg0ire What do you think about that ?

@greg0ire
Copy link
Copy Markdown
Member

greg0ire commented Apr 2, 2026

I agree with you, and furthermore, there are other attributes already: https://github.com/doctrine/DoctrineBundle/tree/3.2.x/src/Attribute

Copy link
Copy Markdown
Member

@greg0ire greg0ire left a comment

Choose a reason for hiding this comment

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

Should this come with docs?

@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch from ea214eb to 7cea86e Compare April 2, 2026 19:55
@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 2, 2026

Should this come with docs?

@greg0ire Documentation added ;)

Comment thread docs/en/dbal-type.rst Outdated
Comment thread docs/en/dbal-type.rst Outdated
@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch from ccf6386 to e92c0b5 Compare April 2, 2026 20:45
Copy link
Copy Markdown
Member

@greg0ire greg0ire left a comment

Choose a reason for hiding this comment

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

To be squash-merged

@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch from e92c0b5 to ef724c7 Compare April 3, 2026 06:11
@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 3, 2026

@greg0ire commits squashed 👍

@GromNaN
Copy link
Copy Markdown
Member

GromNaN commented Apr 3, 2026

I'm working on this topic using a TypeRegistry per connection, so that we can leverage dependency injection.

I want to give a more advanced review before we introduce a solution that will be updated.

In any case, I think it is useful to have an attribute like #[AsType] or #[AsDatabaseType] to specify the type name and the connection or entity manager to which it should be associated.

@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch from f44079e to 51f8380 Compare April 4, 2026 16:46
@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 4, 2026

I'm working on this topic using a TypeRegistry per connection, so that we can leverage dependency injection.

I want to give a more advanced review before we introduce a solution that will be updated.

In any case, I think it is useful to have an attribute like #[AsType] or #[AsDatabaseType] to specify the type name and the connection or entity manager to which it should be associated.

@GromNaN I love the idea of being able to configure types per connection and having them potentially registered as services. The migration path might be long and could be split into several small steps :

  1. Add an #[AsDatabaseType] attribute to improve DX, based on the existing feature (what this PR is supposed to do)
  2. Modify DBAL to enable types per connection configuration (which seem to be implemented in the 2 PRs above)
  3. Integrate this evolution into the bundle
  4. Allow custom types to be defined/registered as services

WDYT ?

I would be happy to help.

In the meantime, I reworked my first implementation to make the attribute name optional, and I renamed the attribute as proposed.

Copy link
Copy Markdown
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

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

I believe 3.2.0 isn't the correct branch anymore because this is a new feature.

Comment thread src/Attribute/AsDbalType.php Outdated
Comment thread docs/en/dbal-type.rst Outdated
Comment thread src/DependencyInjection/Compiler/RegisterDbalTypePass.php Outdated
Comment thread src/DependencyInjection/DoctrineExtension.php Outdated
Comment thread src/DependencyInjection/DoctrineExtension.php Outdated
@GromNaN GromNaN changed the title feat: add AsDbalType attribute feat: add AsDatabaseType attribute Apr 7, 2026
@GromNaN GromNaN changed the title feat: add AsDatabaseType attribute feat: add #[AsDatabaseType] attribute Apr 7, 2026
@GromNaN GromNaN added the Feature label Apr 7, 2026
@GromNaN GromNaN added this to the 3.3.0 milestone Apr 7, 2026
@GromNaN GromNaN changed the base branch from 3.2.x to 3.3.x April 7, 2026 21:54
@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch 2 times, most recently from 8b4961e to ad0ae11 Compare April 7, 2026 22:13
Comment thread src/DependencyInjection/Compiler/RegisterDbalTypePass.php Outdated
Copy link
Copy Markdown
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

That sounds great to me. We'll have to tweak the mechanics for #2221, but without any breaking changes.

@GromNaN GromNaN changed the title feat: add #[AsDatabaseType] attribute Add #[AsDatabaseType] attribute Apr 8, 2026
@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 8, 2026

@GromNaN Is it good for you ? Can I squash my commits ?

@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch from ef4f3c2 to 4f98475 Compare April 8, 2026 11:49
@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 8, 2026

@GromNaN Commits squashed. Many thanks ;)

@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch 2 times, most recently from 9468a0a to 7754d0b Compare April 12, 2026 12:46
@GromNaN GromNaN changed the title Add #[AsDatabaseType] attribute Add #[AsDbalType] attribute Apr 12, 2026
@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch from 7754d0b to f048045 Compare April 12, 2026 16:13
@syl20b syl20b requested a review from greg0ire April 13, 2026 06:47
@syl20b
Copy link
Copy Markdown
Author

syl20b commented Apr 19, 2026

@greg0ire @SenseException is it still Ok for you?

@GromNaN GromNaN merged commit 4644baf into doctrine:3.3.x Apr 20, 2026
11 of 12 checks passed
@GromNaN
Copy link
Copy Markdown
Member

GromNaN commented Apr 20, 2026

Thank you @syl20b

@syl20b syl20b deleted the add-as-doctrine-type-attribute branch April 22, 2026 20:53
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.

7 participants