Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add support for TypeFieldMapper #1696

Open
adrianrudnik opened this issue Aug 13, 2023 · 6 comments · May be fixed by #1779
Open

Feature request: Add support for TypeFieldMapper #1696

adrianrudnik opened this issue Aug 13, 2023 · 6 comments · May be fixed by #1779

Comments

@adrianrudnik
Copy link

Since doctrine/orm#10313 we now have a way to extend the automatic mapping of DBAL types.

Similar to \Doctrine\ORM\Configuration::setNamingStrategy there is now \Doctrine\ORM\Configuration::setTypedFieldMapper, which is not yet configurable by this bundle afaik.

It would be nice to have it configurable with a service, so we can extend it, like we have for the naming_strategy.

My current workaround to get custom types injected is rather cumbersome, and I'm not even sure if my way is to complicated and there is a much easier way to achieve this. I had to circumvent a lot of private service dependencies to get near the configuration stuff, while preserving all injections:

# services.php

$services->set('app.typed_field_mapper', DefaultTypedFieldMapper::class)
    ->args([[CustomField::class => 'customfield']]);

$services->set(Doctrine\ORM\Configuration::class)
    ->public()
    ->decorate('doctrine.orm.default_configuration', 'doctrine.orm.default_configuration.inner')
    ->parent('doctrine.orm.default_configuration')
    ->call('setTypedFieldMapper', [service('app.typed_field_mapper')]);
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 18, 2023

I'd also like to be able to do this. In addition (or instead?) of configuring a service, this could support configuring the mapping directly:

doctrine:
    orm:
        typed_fields:
            App\My\CustomType: my_type

Up to give it a try @adrianrudnik?

@adrianrudnik
Copy link
Author

adrianrudnik commented Aug 18, 2023

@nicolas-grekas I hope I might have a free slot next week.

I always get confused with this topic as doctrine.dbal.types is one-sided and not done in reverse. Not sure it would be actually in the ORM part of the configuration, what do you think?

Example for Symfony:

doctrine:
    dbal:
        mapping_types:
            mycustom: MyCustomType::class

should this not be enough to auto-register stuff for the column definition so that @ORM\Column without a type is correctly mapped to MyCustomType::class if the column type is mycustom and also considered by the SchemaTool to correctly create i.e. doctrine migrations using the correct type (right now it would create a varchar[255] for my type and not consider my registered one)?

For me mycustom is currently (in postgres):

CREATE DOMAIN mycustom AS JSONB;

Looking at your PR, as far as I can understand it quickly, you want to "override" the hard mapping for \DateTime, \DateTimeImmutable and \DateInterval correct? Something like

doctrine:
    dbal:
        mapping_types:
            date_immutable: \Symfony\Component\Clock\DateTime::class
            time_immutable: \Symfony\Component\Clock\DateTime::class
            datetime_immutable: \Symfony\Component\Clock\DateTime::class
            datetimetz_immutable: \Symfony\Component\Clock\DateTime::class

            # if you want to refactor an app that should never use non-immutables anymore
            datetime: \Symfony\Component\Clock\DateTime::class

All _immutables could be overriden by the framework itself, the last one could be done by the user if he wants to enforce immutables?

Not sure I'm getting all of this correctly, just trying to find a common ground.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 18, 2023

TypeFieldMapper is in the Orm namespace, that's why it should be under doctrine.orm.
It provides a mapping from the type on properties to the column type.
That's in ORM because it's a way to define the metadata mapping.

In my case, I would:

  • map properties of type Clock\DateTime to the datetime_immutable type for the ORM side
  • map datetime_immutable to Clock\Doctrine\DateTimeType (to be created) so that all objects get this instead of the native one

@adrianrudnik
Copy link
Author

Ah I see, yes, TypeFieldMapper is indeed in the ORM namespace, getting a bit confused over here.

@ostrolucky
Copy link
Member

Related: #1595

PR welcome

@0x346e3730
Copy link

0x346e3730 commented Dec 2, 2023

Bumping this as DatePoint is released but cannot be used as an entity's propriety for now. I would love to do a PR but I only see how to add typed_fields into the config and don't see where to add doctrine's TypeFieldMapper inside the bundle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants