Skip to content
Advertisement

Generating next sequence value manually in Doctrine 2

What would be the easiest way to generate nextval for some particular sequence with given name?

The annotation solution with specifying

 * @ORMGeneratedValue(strategy="SEQUENCE")
 * @ORMSequenceGenerator(sequenceName="sq_foobar", allocationSize="1", initialValue="1")

doesn’t satisfy me, as long as there is some more complex logic involved: in some cases I need to retrieve nextval, in other – I would go with the value retrieved from another sources (not sequence).

So I hope there is a way to retrieve a sequence nextval manually in entity’s constructor.

Advertisement

Answer

Then I think you should implement your own Identitfer Generator.

The easyest would be to override the DoctrineORMIdSequenceGenerator class to handle your specific case.

You then have to register this generator in the class metadata using Doctrine ORM API.

Some links: http://ranskills.wordpress.com/2011/05/26/how-to-add-a-custom-id-generation-strategy-to-doctrine-2-1/

https://github.com/doctrine/doctrine2/pull/206

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement