doctrine2:如何在不丢失数据的情况下将一对多转换为多对多转换

时间:2015-05-23 11:11:29

标签: mysql symfony doctrine-orm doctrine entity-relationship

在我的应用程序中,我希望将一对多转换为多对多而不会丢失数据:

从:

/**
 * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\FoodAnalytics\Recipe", inversedBy="medias")
 * @ORM\JoinColumn(name="recipeId", referencedColumnName="id", onDelete="CASCADE")
 */
protected $recipe;

为:

/**
 * @ORM\ManyToMany(targetEntity="\AppBundle\Entity\FoodAnalytics\Recipe", inversedBy="medias")
 * @ORM\JoinTable(
 *     name="media_recipes",
 *     joinColumns={@ORM\JoinColumn(name="mediaId", referencedColumnName="id", onDelete="CASCADE")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="recipeId", referencedColumnName="id", onDelete="CASCADE")}
 * )
 */
protected $recipes;

当我放弃我的学说谢谢更新时,它说它会丢弃数据,而不是我想要的数据:

CREATE TABLE media_recipes (mediaId INT UNSIGNED NOT NULL, recipeId INT UNSIGNED NOT NULL, INDEX IDX_C2BE64FC27D9F5AC (mediaId), INDEX IDX_C2BE64FC6DCBA54 (recipeId), PRIMARY KEY(mediaId, recipeId)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE media_recipes ADD CONSTRAINT FK_C2BE64FC27D9F5AC FOREIGN KEY (mediaId) REFERENCES media (id) ON DELETE CASCADE;
ALTER TABLE media_recipes ADD CONSTRAINT FK_C2BE64FC6DCBA54 FOREIGN KEY (recipeId) REFERENCES Recipe (id) ON DELETE CASCADE;
ALTER TABLE media DROP FOREIGN KEY FK_6A2CA10C6DCBA54;
DROP INDEX IDX_6A2CA10C6DCBA54 ON media;
ALTER TABLE media DROP recipeId;

Process finished with exit code 0 at 13:04:46.
Execution time: 5 003 ms.

我该如何解决这个问题?即将配方表的前一个mysql mediaId列与配方ID一起添加到新的media_recipe表中?

3 个答案:

答案 0 :(得分:7)

好的,我确实找到了一种使用学说迁移的方法。

我首先确保使用doctrine正确设置我的更改:schema:update --force,检查分析器,然后恢复到最后一个数据库。

所以使用doctrine migrations bundle:

doctrine:migrations:diff

创建一个新的迁移类

编辑课程以满足您的需求。我的是:

<?php

namespace Application\Migrations;

use AppBundle\Entity\Core\Media;
use AppBundle\Entity\FoodAnalytics\Recipe;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * 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;
    }

    /**
     * @param Schema $schema
     */
    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)
        {
            $mediaId = $row['mediaId'];
            $recipeId = $row['recipeId'];
            $this->customSQL[] = "($mediaId, $recipeId)";
        }

    }

    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $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_C2BE64FC27D9F5AC (mediaId), INDEX IDX_C2BE64FC6DCBA54 (recipeId), PRIMARY KEY(mediaId, recipeId)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
        $this->addSql('ALTER TABLE media_recipes ADD CONSTRAINT FK_C2BE64FC27D9F5AC FOREIGN KEY (mediaId) REFERENCES media (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE media_recipes ADD CONSTRAINT FK_C2BE64FC6DCBA54 FOREIGN KEY (recipeId) REFERENCES Recipe (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE media DROP FOREIGN KEY FK_6A2CA10C6DCBA54');
        $this->addSql('DROP INDEX IDX_6A2CA10C6DCBA54 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);
    }



    /**
     * @param Schema $schema
     */
    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your needs
        $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_6A2CA10C6DCBA54 FOREIGN KEY (recipeId) REFERENCES recipe (id) ON DELETE CASCADE');
        $this->addSql('CREATE INDEX IDX_6A2CA10C6DCBA54 ON media (recipeId)');
    }
}

然后执行更新:

doctrine:migrations:migrate

并确认y

答案 1 :(得分:2)

如果您想继续使用orm:schema-tool:update --force而不是采用doctrine:migrations:migrate,那么您可以使用两步流程将关系从多对一迁移到多对多。

  1. 创建many-to-many-step1分支
    • 在旧的@ ManyToOne / @ JoinColumn / @ OneToMany属性和访问者旁边添加新的@ ManyToMany / @ JoinTable属性和访问器
    • 部署代码并运行orm:schema-tool:update --force
    • 使用INSERT INTO ... SELECT语句
    • 使用旧连接列内容填充新连接表
  2. 创建many-to-many-step2分支
    • 更改代码以使用新的多对多关系和访问者
    • 删除旧的多对一关系的所有工件
    • 部署代码并运行orm:schema-tool:update --force

答案 2 :(得分:1)

我已经解决了这一问题,只需使用php bin/console make:migration生成一个迁移,然后使用INSERT语句扩展该迁移,该语句将所有数据从旧的oneToMany字段复制到新的manyToMany表中,然后删除旧的列。

为遵循问题中的媒体/食谱示例,我将在常规迁移的$this->addSql('ALTER TABLE media DROP recipeId');行之前添加以下行:

$this->addSql('INSERT INTO media_recipes (mediaId, recipeId) SELECT id, recipeId FROM media WHERE recipeId IS NOT NULL');