如何使用doctrine2设置初始auto_increment值

时间:2011-11-05 19:18:08

标签: php mysql doctrine-orm

我使用doctrine2映射器生成我的innoDB(mysql)数据库。 如何使用php注释设置auto_incremented id的初始值?

这就是我目前为实体类型建模的方式。

/**
 * @var integer $_id
 *
 * @Column(name="id", type="integer", nullable=false)
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 */
private $_id;

我在文档中找到了以下代码,但看起来好像会使用单独的表来生成ID。

/**
 * @Id
 * @GeneratedValue(strategy="SEQUENCE")
 * @Column(type="integer")
 * @SequenceGenerator(sequenceName="tablename_seq", initialValue=1, allocationSize=100)
 */

3 个答案:

答案 0 :(得分:2)

您可以设置strategy =“NONE”并在@prepersist函数中设置最后一个ID。更容易的是只添加一个“ALTER TABLE something AUTO_INCREMENT = 100;”在DataFixture或数据库迁移中。它不是可移植的SQL,但它可以在不增加实体复杂性的情况下完成工作。

答案 1 :(得分:1)

从文档中不是很清楚,但消息来源说......

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']);

所以mysql作为@table注释的选项

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

答案 2 :(得分:0)

这是在MySQL的准则2中设置auto_increment的完整代码示例:

$connection = $this->getEntityManager()->getConnection();
$connection->prepare('ALTER TABLE my_table AUTO_INCREMENT = 100;')->execute();
相关问题