Generating next sequence value manually in Doctrine 2(在 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 中手动生成下一个序列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Doctrine 2 中手动生成下一个序列值
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01