在 Doctrine 2 中手动生成下一个序列值

2022-01-03 00:00:00 php doctrine-orm

为具有给定名称的特定序列生成 nextval 的最简单方法是什么?

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

带指定的注解方案

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

不满足我,只要涉及一些更复杂的逻辑:在某些情况下我需要检索 nextval,在其他情况下 - 我会使用从其他来源检索的值(不是顺序).

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).

所以我希望有一种方法可以在实体的构造函数中手动检索序列 nextval.

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

推荐答案

那么我认为你应该实现自己的 Identitfer Generator.

Then I think you should implement your own Identitfer Generator.

最简单的方法是覆盖 DoctrineORMIdSequenceGenerator 类来处理您的特定情况.

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

然后,您必须使用 Doctrine ORM API 在类元数据中注册此生成器.

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

一些链接: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

相关文章