在Doctrine 2中手动生成下一个序列值 [英] Generating next sequence value manually in Doctrine 2
问题描述
对于某些具有给定名称的特定序列,最简单的方法是生成 nextval
解决方案指定
* @ ORM\GeneratedValue(strategy =SEQUENCE)
* @ ORM\SequenceGenerator (sequenceName =sq_foobar,allocateSize =1,initialValue =1)
只要有一些更复杂的逻辑涉及到:在某些情况下,我需要检索 nextval
,其他 - 我会从其他来源检索到的值(不是序列)
所以我希望有一种方法可以在实体的构造函数中手动检索一个序列nextval。
然后我想你应该实现自己的Identitfer生成器。
最容易的是覆盖Doctrine\ORM\\ \\Id\SequenceGenerator类来处理你的具体情况。
然后你必须重新使用Doctrine ORM API在类元数据中提供此生成器。
https://github.com/doctrine/doctrine2/pull/206
What would be the easiest way to generate nextval
for some particular sequence with given name?
The annotation solution with specifying
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(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.
Then I think you should implement your own Identitfer Generator.
The easyest would be to override the Doctrine\ORM\Id\SequenceGenerator 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
这篇关于在Doctrine 2中手动生成下一个序列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!