Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an initial auto_increment value using doctrine2

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)
 */
like image 514
Jakob Alexander Eichler Avatar asked Nov 05 '11 19:11

Jakob Alexander Eichler


2 Answers

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();
like image 198
Sebastian Viereck Avatar answered Oct 24 '22 14:10

Sebastian Viereck


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.

like image 31
tvlooy Avatar answered Oct 24 '22 16:10

tvlooy