Doctrine 2添加自动生成序列值的新字段 [英] Doctrine 2 add new field that auto generates sequence values

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

问题描述

我有一个从另一个表(类B)继承的表(类A),所以显然表A的主键引用表B的主键。但是,我想要做的是向表A添加一个新字段,该字段应该自动递增,并启动一个特定的值,例如。 1000001.



我已经尝试了多种方法在类A中添加这个新字段(列),但是我无法让它工作:

  A类扩展B {
/ **
* @var整数
*
* @ORM \GeneratedValue(strategy =Identity)
* @ ORM\Column(type =integer)
* @ ORM\SequenceGenerator(sequenceName =beer_id,allocateSize = 1,initialValue = 1000001)
** /
private $ beerIdNumber;

如何使这个新列自动生成序列值,并可能使其成为附加主键列还是唯一值? (我的数据库是MySql)

解决方案

Doctrine 2文档


序列生成器目前可以与
Oracle或Postgres一起使用,并允许一些额外的配置选项
,除了指定序列名称


似乎对于你所有你需要使用的是使用:

  A类扩展B {

/ **
* @var整数
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =Identity)
* @ ORM\Column(type =integer)
* /
private $ beerIdNumber;

另外从文档


@GeneratedValue指定哪个策略用于由@Id注释的实例变量的标识符
生成。这个
注释是可选的,只有在
与@Id结合使用时才有意义。



如果未使用@Id指定该注释,则NONE策略使用
作为默认值。


HOWEVER



复合键


Doctrine 2允许使用复合主键。然而,
有一些限制与使用单个标识符相反。
@GeneratedValue注释的使用仅支持简单(不是
复合)主键,这意味着您只能使用复合键
,如果您在调用$之前自动生成主键值b $ b实体上的EntityManager#persist()。



要指定复合主键/标识符,只需将@Id
标记注释放在所有字段这是主键。



I have a table (class A) that inherits from another table (class B), so obviously table A's primary key references table B's primary key. However, what I'm trying to do is add a new field to table A that should auto increment and also start a specific value, ex. 1000001.

I have tried multiple ways off adding this new field (column) in class A, but I can't get it to work:

Class A extends B {
/**
 * @var integer
 *
 * @ORM\GeneratedValue(strategy="Identity")
 * @ORM\Column(type="integer")
 * @ORM\SequenceGenerator(sequenceName="beer_id", allocationSize=1, initialValue=1000001)
 **/
private $beerIdNumber;

How can I make this new column auto generate sequence values, and possibly make it into an additional primary key column or be unique value? (my database is MySql)

解决方案

From the Doctrine 2 Documentation:

The Sequence Generator can currently be used in conjunction with Oracle or Postgres and allows some additional configuration options besides specifying the sequence’s name

It would seem that for you all you need to use is use:

Class A extends B {

/**
 * @var integer
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="Identity")
 * @ORM\Column(type="integer")
 */
 private $beerIdNumber;

Also From the documentation:

@GeneratedValue Specifies which strategy is used for identifier generation for an instance variable which is annotated by @Id. This annotation is optional and only has meaning when used in conjunction with @Id.

If this annotation is not specified with @Id the NONE strategy is used as default.

HOWEVER

Composite Keys

Doctrine 2 allows to use composite primary keys. There are however some restrictions opposed to using a single identifier. The use of the @GeneratedValue annotation is only supported for simple (not composite) primary keys, which means you can only use composite keys if you generate the primary key values yourself before calling EntityManager#persist() on the entity.

To designate a composite primary key / identifier, simply put the @Id marker annotation on all fields that make up the primary key.

这篇关于Doctrine 2添加自动生成序列值的新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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