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?
我使用doctrine2 mapper生成我的innoDB (mysql)数据库。如何使用php注释设置我的auto_increment id的初始值?
This is how I modelled the id of my entity type at the moment.
这就是我当时建模实体类型id的方法。
/**
* @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。
/**
* @Id
* @GeneratedValue(strategy="SEQUENCE")
* @Column(type="integer")
* @SequenceGenerator(sequenceName="tablename_seq", initialValue=1, allocationSize=100)
*/
2 个解决方案
#1
2
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.
您可以设置strategy="NONE"并在@prepersist函数中设置最后一个ID。更简单的方法是在DataFixture或DB迁移中添加“ALTER TABLE something AUTO_INCREMENT = 100”。它不是可移植的SQL,但是它可以在不增加实体复杂性的情况下完成这项工作。
#2
1
not really clear from documentation, but sources says ...
从文件上看不太清楚,但是有消息说……
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 works as option for @table annotation
所以mysql作为@table注释的选项。
* @ORM\Table(name="xxx", options={"auto_increment": 100})
#1
2
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.
您可以设置strategy="NONE"并在@prepersist函数中设置最后一个ID。更简单的方法是在DataFixture或DB迁移中添加“ALTER TABLE something AUTO_INCREMENT = 100”。它不是可移植的SQL,但是它可以在不增加实体复杂性的情况下完成这项工作。
#2
1
not really clear from documentation, but sources says ...
从文件上看不太清楚,但是有消息说……
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 works as option for @table annotation
所以mysql作为@table注释的选项。
* @ORM\Table(name="xxx", options={"auto_increment": 100})