Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Auto Increment Starting Value @ORM\GeneratedValue

How can I set the starting value for an Auto Incremented id using annotations ?

I want it to start at 250000

/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
protected $id;
like image 818
NickOS Avatar asked Aug 07 '15 00:08

NickOS


3 Answers

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,

/**
 * @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

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.

like image 102
Kiran Avatar answered Nov 18 '22 15:11

Kiran


/**
 * @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

like image 21
dlporter98 Avatar answered Nov 18 '22 13:11

dlporter98


On MySQL platform, I write that (@ORM\Table(options={"auto_increment": 12345})) to add "AUTO_INCREMENT = 12345" to the SQL creation table. I didn't use @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');
like image 7
kouinkouin Avatar answered Nov 18 '22 13:11

kouinkouin