D
D
Dmitry Vapelnik2015-08-24 01:00:41
MySQL
Dmitry Vapelnik, 2015-08-24 01:00:41

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());

Thus, I can now set my IDs. But after that, I need new categories to be added with the generation of new IDs, which should not overlap with existing ones. I don’t really want to generate IDs for all models on my own, at least because there will be unnecessary queries to the database. There will not be many of them, but, in a good way, they should not be done. So, when I had only one table and nothing was connected with it (localized records table), I did the following:
1. The table for categories was initially generated with AUTO_INCREMENT
2. Before I started converting categories, I removed AUTO_INCREMENT from the table and the above code changed the ID generation strategy for this model.
3. Converted records, added them to the table
4. After converting the categories, I created a new table with the category table structure (1), added AUTO_INCREMENT for ID (2), set the AUTO_INCREMENT value for the new table to {MAX_CATEGORY_ID + 1} (3), transferred the converted records from the old table to the new one ( 4), deleted the old table (5), renamed the new table to the old one.
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}`;"
        );
}

This solution worked until I added the localization for the categories and put it in a separate table.
I tried first setting AUTO_INCREMENT to a value that exceeds the max(ID) of the categories and setting the ID generation strategy in doctrine to manual (while setting the table to AUTO_INCREMENT for the key), but then the error was thrown here .
If you set the value to AUTO_INCREMENT, but do not change the ID generation strategy, then all values ​​will be inserted with an ID that exceeds the value of AUTO_INCREMENT set.
I assume that this problem would come out later - when I start adding content to categories - it will be attached to categories and communication keys will be used.
Now I put everything together and form the resulting question: what way to go so that you can correctly insert records into a table with preset keys and records into a related table with a foreign key and subsequently be able to use AUTO_INCREMENT for the primary key of the table?
Perhaps I have formed something wrong - I will answer questions in the comments.
UPDATE
As far as I understand, a cleaner and more correct solution would be to create your own ID generator (which will generate an ID like {max(id_on_table) + 1} if the entity does not have an ID and return a narrowness ID if it is) and use it to convertible models

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question