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

@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
Author

syl20b commented Mar 11, 2026

@stof is it ok for you?

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.

3 participants