How to set auto_increment to initial value using doctrine2

I am using mapper for doctrine2 to create my innoDB database (mysql). How to set the initial value of my auto_incremented id using php annotations?

This is how I modeled the identifier 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 like it will use a separate table to create identifiers.

 /** * @Id * @GeneratedValue(strategy="SEQUENCE") * @Column(type="integer") * @SequenceGenerator(sequenceName="tablename_seq", initialValue=1, allocationSize=100) */ 
+6
source share
3 answers

You can set the strategy = "NONE" and set the last identifier in the @prepersist function. It would be easier to just add "ALTER TABLE something AUTO_INCREMENT = 100;" in a DataFixture or DB migration. This is not portable SQL, but it does the job without adding complexity to your Entity.

+2
source

not entirely clear from the documentation, but sources say ...

 doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php: if (isset($options['auto_increment'])) { doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php: $tableOptions[] = sprintf('AUTO_INCREMENT = %s', $options['auto_increment']); 

so for mysql it works as an option for @table annotations

 * @ORM\Table(name="xxx", options={"auto_increment": 100}) 
+1
source

Here is a complete code example for setting auto_increment in doctrine 2 in MySQL:

 $connection = $this->getEntityManager()->getConnection(); $connection->prepare('ALTER TABLE my_tableAUTO_INCREMENT = 100;')->execute(); 
0
source

Source: https://habr.com/ru/post/900855/


All Articles