Answer the question
In order to leave comments, you need to log in
How to change AUTO_INCREMENT on a table linked to another?
I'm writing a converter to convert records from an old database to a new one using Symfony and A2lixI18nDoctrineBundle
In the old database, titles in two languages were stored in one table. Now one table will be used to store the category tree and a second table for category names in two languages. The problem is this: I need to insert elements into the database using Doctrine with IDs already set. I solved this with the following code:
$classMetadata = $this->getClassMetadata($entity);
$classMetadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$classMetadata->setIdGenerator(new AssignedGenerator());
private function setAutoincrementValue($entity, $autoincrement)
{
$tableName = $this->getClassMetadata($entity)->getTableName();
$tableNameNew = $tableName.'_new';
$this->getEntityManager()->getConnection()
->exec(
"DROP TABLE IF EXISTS `{$tableNameNew}`;
CREATE TABLE `{$tableNameNew}` LIKE `{$tableName}`;
ALTER TABLE `{$tableNameNew}` CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `{$tableNameNew}` AUTO_INCREMENT = {$autoincrement};
INSERT INTO `{$tableNameNew}` SELECT * FROM {$tableName};
DROP TABLE `{$tableName}`;
RENAME TABLE `{$tableNameNew}` TO `{$tableName}`;"
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question