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

查看:16
本文介绍了在 Doctrine 2 中手动生成下一个序列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为具有给定名称的特定序列生成 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

这篇关于在 Doctrine 2 中手动生成下一个序列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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