我使用doctrine2映射器生成我的innoDB (mysql)数据库。如何使用auto_incremented注解设置我的php id的初始值?
这就是我当时对实体类型的id进行建模的方式。
/**
* @var integer $_id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $_id;
我在文档中找到了以下代码,但看起来它似乎要使用一个单独的表来生成if。
/**
* @Id
* @GeneratedValue(strategy="SEQUENCE")
* @Column(type="integer")
* @SequenceGenerator(sequenceName="tablename_seq", initialValue=1, allocationSize=100)
*/
发布于 2011-11-13 17:39:58
您可以将strategy=设置为“NONE”,并在@prepersist函数中设置最后一个ID。更简单的做法是在DataFixture或DB迁移中添加"ALTER TABLE something AUTO_INCREMENT = 100;“。它不是可移植的SQL,但它可以在不增加实体复杂性的情况下完成这项工作。
发布于 2017-08-10 03:36:00
从文档上看不是很清楚,但是有消息说...
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']);
因此for mysql作为@table注解的选项
* @ORM\Table(name="xxx", options={"auto_increment": 100})
发布于 2019-05-03 22:37:50
下面是在MySQL的规则2中设置auto_increment的完整代码示例:
$connection = $this->getEntityManager()->getConnection();
$connection->prepare('ALTER TABLE my_table AUTO_INCREMENT = 100;')->execute();
https://stackoverflow.com/questions/8022629
复制相似问题