Orm: Add constructor parameters in CustomIdGenerator annotation

Created on 10 Feb 2016  路  8Comments  路  Source: doctrine/orm

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:

@CustomIdGenerator(class="MyBundle\Doctrine\MyGenerator", arguments={"arg1","arg2"})

It will be working with the following class:

namespace MyBundle\Doctrine;

use Doctrine\ORM\Id\AbstractIdGenerator;
use Doctrine\ORM\EntityManager;

class MyGenerator extends AbstractIdGenerator
{
    public function __construct($arg1, $arg2)
    {
        // do something here
    }

    public function generate(EntityManager $em, $entity)
    {
        // do something here
    }

}
Improvement

Most helpful comment

Sure, but how do I pass different arguments for different entities?

You don't, you simply code in a more specific way:

@CustomIdGenerator(class="MyBundle\Doctrine\CatGenerator")
@CustomIdGenerator(class="MyBundle\Doctrine\DogGenerator")

All 8 comments

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)

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?

Sure, but how do I pass different arguments for different entities?

You don't, you simply code in a more specific way:

@CustomIdGenerator(class="MyBundle\Doctrine\CatGenerator")
@CustomIdGenerator(class="MyBundle\Doctrine\DogGenerator")

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.

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.

@aledeg that said, the issue could stay open if we have a way to inject ID generator _instances_

I am looking forward to see that implemented :)

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

strayobject picture strayobject  路  4Comments

doctrinebot picture doctrinebot  路  3Comments

weaverryan picture weaverryan  路  3Comments

doctrinebot picture doctrinebot  路  3Comments

podorozhny picture podorozhny  路  4Comments