简体   繁体   English

Doctrine Auto Increment起始值@ORM \\ GeneratedValue

[英]Doctrine Auto Increment Starting Value @ORM\GeneratedValue

How can I set the starting value for an Auto Incremented id using annotations ? 如何使用注释设置自动增量ID的起始值?

I want it to start at 250000 我希望它从250000开始

/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
protected $id;
/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="SEQUENCE")
 * @ORM\SequenceGenerator(sequenceName="id", initialValue=250000)
 * @ORM\Column(type="integer")
 */
 protected $id;

http://doctrine-orm.readthedocs.org/en/latest/reference/basic-mapping.html http://doctrine-orm.readthedocs.org/en/latest/reference/basic-mapping.html

Don't know this answer is useful to users or not. 不知道这个答案对用户有用。 But for me its giving required result (for docrtine with mysql) I had used auto increment id as normally used, 但对我来说,它给出了所需的结果(对于使用mysql的docrtine)我使用了通常使用的自动增量id,

/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

First time you run command to update than table will create. 第一次运行命令更新而不是表将创建。 Than you should manually run below command to set custom initial auto increment id 你应该手动运行以下命令来设置自定义初始自动增量ID

ALTER TABLE users AUTO_INCREMENT=1001;

Again on running schema it will not update auto increment which is manually set. 再次在运行模式时,它不会更新手动设置的自动增量。 And thats it!! 就是这样!! Its the desired result. 它是理想的结果。

On MySQL platform, I write that ( @ORM\\Table(options={"auto_increment": 12345}) ) to add "AUTO_INCREMENT = 12345" to the SQL creation table. 在MySQL平台上,我写了( @ORM\\Table(options={"auto_increment": 12345}) )将“AUTO_INCREMENT = 12345”添加到SQL创建表中。 I didn't use @ORM\\SequenceGenerator : 我没有使用@ORM\\SequenceGenerator

/*
 * @ORM\Table(options={"auto_increment": 12345})
 */
class MyEntity {

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    // Others properties
}

And in the migration file: 并在迁移文件中:

    $this->addSql('CREATE TABLE my_entity (id INT AUTO_INCREMENT NOT NULL, ...) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB AUTO_INCREMENT = 12345');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM