I use the doctrine2 mapper to generate my innoDB (mysql) database. How to set the initial value of my auto_incremented id using the php annotations?
This is how I modelled the id of my entity type at the moment.
/**
* @var integer $_id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $_id;
I found the following code in the documentation but it looks as if it would use a separate table to generate the ids.
/**
* @Id
* @GeneratedValue(strategy="SEQUENCE")
* @Column(type="integer")
* @SequenceGenerator(sequenceName="tablename_seq", initialValue=1, allocationSize=100)
*/
Here is the complete code example for setting the auto_increment in doctrine 2 in MySQL:
$connection = $this->getEntityManager()->getConnection();
$connection->prepare('ALTER TABLE my_table AUTO_INCREMENT = 100;')->execute();
You could set strategy="NONE" and set the last ID in a @prepersist function. More easy would be to just add a "ALTER TABLE something AUTO_INCREMENT = 100;" in a DataFixture or DB migration. It's not portable SQL but it does the job without adding complexity in your Entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With