Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

Doctrine2: One-to-Many ohne Datenverlust in Many-to-Many umwandeln

OK, ich habe einen Weg über Doctrine-Migrations gefunden.

Zuerst habe ich mit doctrine:schema:update --force sichergestellt, dass meine Änderungen korrekt sind, den Profiler geprüft und dann auf die letzte Datenbank zurückgesetzt.

Mit dem Doctrine-Migrations-Bundle erzeugt

doctrine:migrations:diff

eine neue Migrationsklasse.

Die Klasse nach Bedarf anpassen. Bei mir sah sie so aus:

<?php
namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20150525154902 extends AbstractMigration implements ContainerAwareInterface
{
    private $customSQL = array();

    /** @var Container */
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function preUp(Schema $schema)
    {
        $query = "SELECT id as mediaId, recipeId FROM `media` WHERE recipeId IS NOT NULL";
        $data = $this->connection->prepare($query);
        $data->execute();
        foreach ($data as $row) {
            $this->customSQL[] = "(" . $row['mediaId'] . ", " . $row['recipeId'] . ")";
        }
    }

    public function up(Schema $schema)
    {
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql',
            'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('CREATE TABLE media_recipes (mediaId INT UNSIGNED NOT NULL, recipeId INT UNSIGNED NOT NULL, INDEX IDX_MEDIA (mediaId), INDEX IDX_RECIPE (recipeId), PRIMARY KEY(mediaId, recipeId)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
        $this->addSql('ALTER TABLE media_recipes ADD CONSTRAINT FK_MEDIA FOREIGN KEY (mediaId) REFERENCES media (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE media_recipes ADD CONSTRAINT FK_RECIPE FOREIGN KEY (recipeId) REFERENCES Recipe (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE media DROP FOREIGN KEY FK_RECIPE_MEDIA');
        $this->addSql('DROP INDEX IDX_RECIPE_MEDIA ON media');
        $this->addSql('ALTER TABLE media DROP recipeId');
        $this->addSql('ALTER TABLE recipe ADD versionDetails VARCHAR(200) DEFAULT NULL');
    }

    public function postUp(Schema $schema)
    {
        $SQL = 'INSERT INTO media_recipes (mediaId, recipeId) VALUES ' . implode(', ', $this->customSQL);
        $this->connection->executeQuery($SQL);
    }

    public function down(Schema $schema)
    {
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql',
            'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('DROP TABLE media_recipes');
        $this->addSql('ALTER TABLE media ADD recipeId INT UNSIGNED DEFAULT NULL');
        $this->addSql('ALTER TABLE media ADD CONSTRAINT FK_RECIPE_MEDIA FOREIGN KEY (recipeId) REFERENCES recipe (id) ON DELETE CASCADE');
        $this->addSql('CREATE INDEX IDX_RECIPE_MEDIA ON media (recipeId)');
    }
}

Der entscheidende Punkt: preUp() sichert die alten Zuordnungen, up() baut die Zwischentabelle, und postUp() füllt sie wieder ein.

Dann die Aktualisierung ausführen:

doctrine:migrations:migrate

und mit y bestätigen.