acelaya / doctrine-enum-type

A custom Doctrine type that maps column values to enum objects using myclabs/php-enum
MIT License
132 stars 15 forks source link

Q: registering enums for schema operations needed? #47

Closed nikolaposa closed 4 years ago

nikolaposa commented 4 years ago

There is one bit of documentation that confuses me. :) So in the README it says:

// Don't forget to register the enums for schema operations
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('VARCHAR', Action::class);
$platform->registerDoctrineTypeMapping('VARCHAR', 'php_enum_gender');

But what does this part actually do? Looking at the Doctrine DBAL code: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php#L341, after the above code is executed, only the last enum (php_enum_gender) will be mapped to VARCHAR, right?

Ultimately I feel like registering these type mappings is not required in order for things to work properly at the Type level which is what PhpEnumType::registerEnumType() does.

acelaya commented 4 years ago

I'm afraid I don't have a direct answer for this 😅

I remember there was an issue opened some time ago because of this.

I will have to dig into the issues to remember exactly what was the problem, and then improve the docs, maybe.

Anyway, if everything works for you without adding that, I think you are good 🙂

nikolaposa commented 4 years ago

Yeah, current setup works even without that part, so that's why I was wondering what is the idea behind it, what it was supposed to do. 🤔

acelaya commented 4 years ago

If I had to guess, I would say it was related with some of those doctrine commands which generate tables based on entities.

Or maybe it was related with migrations.

It was something in those lines 😅

I will review later today when I get home.

nikolaposa commented 4 years ago

Sounds good, thanks!

acelaya commented 4 years ago

@nikolaposa this was the reason to document that https://github.com/acelaya/doctrine-enum-type/issues/23

nikolaposa commented 4 years ago

Ah, interesting, I'll investigate further. But that answers my actual question here so closing this one. Thank you!

acelaya commented 4 years ago

I will probably try to provide further details in the docs, maybe even linking to the issue.

antonioturdo commented 4 years ago

I am not sure, but I think registerDoctrineTypeMapping method allows reverse engineering from a db schema to doctrine representation. The method writes an associative array where the first argument is used as key. If my assumption is correct the method instructs doctrine how to translate a db type in a doctrine type. So, as @nikolaposa said, if you call that method twice with an equal value of the first argument, the second time you are just overriding first call. Since it doesn't make sense to translate a varchar to a specific enum, I think the documentation is wrong. I'm still not sure that calling the method is necessary, but possibly the first parameter should be a fictitious one, for example the name of the enumeration.

tsadiq commented 3 years ago

FWIW, i needed to add this so Doctrine Migrations would work properly. So if anyone ends up here like me trying to fix this for symfony or api-platform, you just need to add some subscriber on migrations events and register your enum types there 👌