9

当我使用./bin/doctrine orm:fixtures:load示例数据填充表时,首先迁移设置自动增量表 ID,如 1、2、3、4、5 等...

在第二个orm:fixtures:load迁移命令之后,它会清除所有数据并设置 5、6、7、8、9 等 ID...

当我多次加载灯具时,如何将 AI id 计数器重置为 1?

4

2 回答 2

21

$ app/console help doctrine:fixtures:load

默认情况下,Doctrine Data Fixtures 使用 DELETE 语句从数据库中删除现有行。如果要改用 TRUNCATE 语句,可以使用 --purge-with-truncate 标志:

./app/console doctrine:fixtures:load --purge-with-truncate

截断将重置自动增量。

更新

控制台命令适用于 Symfony,但仅使用 Doctrine 时应该是相同的:

./bin/doctrine orm:fixtures:load --purge-with-truncate

更新#2关于抛出异常的评论

如果您有外键,则只能AUTO_INCREMENT通过常规 SQL 重置:

$connection = $this->getEntityManager()->getConnection();
$connection->exec("ALTER TABLE <tablename> AUTO_INCREMENT = 1;");
于 2012-09-17T08:23:25.320 回答
1

在 Zend 2 中,我使用了下面提到的代码来截断自动增量值并将其重置为 1。在您的存储库中使用此代码。

// To delete all records
$queryBuilder = $this->createQueryBuilder('c');
$queryBuilder->delete();
$queryBuilder->getQuery()->execute();

//Reset table auto increment.
$tableName = $this->getClassMetadata()->getTableName();
$connection = $this->getEntityManager()->getConnection();
$connection->exec("ALTER TABLE " . $tableName . " AUTO_INCREMENT = 1;");
于 2017-04-21T04:52:09.270 回答