在Doctrine 2中手动生成下一个序列值 [英] Generating next sequence value manually in Doctrine 2

查看:128
本文介绍了在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在类元数据中提供此生成器。



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


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆