Skip to content

feat: add AsDbalType attribute#2197

Open
syl20b wants to merge 7 commits intodoctrine:3.2.xfrom
syl20b:add-as-doctrine-type-attribute
Open

feat: add AsDbalType attribute#2197
syl20b wants to merge 7 commits intodoctrine:3.2.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.

@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch from 3304bbe to 8f59bd1 Compare January 30, 2026 23:55
@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch 2 times, most recently from 8d6c4f7 to e26bfe1 Compare February 1, 2026 21:15
@syl20b syl20b force-pushed the add-as-doctrine-type-attribute branch 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
@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,...)

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.

4 participants