mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Add constructor parameters in CustomIdGenerator annotation #5015
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @aledeg on GitHub (Feb 10, 2016).
It would be nice to be able to add parameters to the constructor to change the generator behavior. In my case, I would like to inject the length of the generated value.
In that case, we could use the following definition in the annotations:
It will be working with the following class:
@Ocramius commented on GitHub (Feb 10, 2016):
Since it is a custom generator, why not make the arguments part of the constructor defaults? What is the advantage of pushing this config into the mapping? (you still don't get access to external services anyway)
@aledeg commented on GitHub (Feb 10, 2016):
Sure, but how do I pass different arguments for different entities?
If I want to generate an unique string but with different length for different entities using the same generator, how do I do that?
@Ocramius commented on GitHub (Feb 10, 2016):
You don't, you simply code in a more specific way:
@Ocramius commented on GitHub (Feb 10, 2016):
The point is: unless defining a custom generator with proper constructor injection is possible, allowing just config injection will simply lead to a half-baked solution.
@aledeg commented on GitHub (Feb 10, 2016):
I see your point.
So I need to create an abstract class defining all my logic. And then I create classes with different implementations with different parameters.
@Ocramius commented on GitHub (Feb 10, 2016):
@aledeg that said, the issue could stay open if we have a way to inject ID generator instances
@aledeg commented on GitHub (Feb 10, 2016):
I am looking forward to see that implemented :)
@Craige commented on GitHub (May 4, 2017):
Any word on this? I think it would be very useful to be able to inject generator instances into the
@CustomIdGenerator. The generator is pretty inflexible without that ability.For example, if I want my generator to do something intensive and cache the result for future use, I would have to hard code in the cache directory rather than pass it in from the service container. What I would like to do is instantiate and pass configuration options to my custom Generator, and then pass that Generator instance to the
@CustomIdGenerator@nicolas-grekas commented on GitHub (Jan 28, 2021):
This is implemented in v3 apparently.