gpt4 book ai didi

doctrine - 删除 postUp() 方法中的列

转载 作者:行者123 更新时间:2023-12-03 06:27:23 25 4
gpt4 key购买 nike

是否可以在迁移 postUp() 方法中删除列或仅用于数据操作?

最佳答案

我不知道您使用的是哪个版本的 Doctrine Migrations。然而,我在使用 Doctrine Migrations 2.0 时遇到了同样的问题,并开始深入研究代码。当查看 Version 的构造方式时,发现使用 Schema 对象在 postUp() 方法中进行更改似乎不会生效。

不过,仔细想想,这确实是有道理的。每个迁移版本都旨在修改数据库结构。这发生在每次迁移的 up() 和 down() 方法中。 postUp() 似乎主要用于结构更改后清理(即操作数据)。您打算在 postUp() 方法中进行的任何进一步的结构修改都应该在后续的迁移文件中进行。

例如,我试图创建一个新表,该表将保存上一个表中的两列。在将数据迁移到新表后,我打算从前一个表中删除这些列。代码如下:

class Version20110512223208 extends AbstractMigration
{
protected $customerRepository;

protected $evernoteRepository;

public function up(Schema $schema)
{
$table = $schema->createTable('customer_evernote');
$table->addOption('type', 'INNODB');
$table->addOption('charset', 'utf8');
$table->addOption('collate', 'utf8_unicode_ci');

// Columns.
$table->addColumn('customer_id', 'bigint', array(
'length' => 20,
'notnull' => true,
'autoincrement' => false));
$table->addColumn('integration_date', 'datetime', array('notnull' => true));
$table->addColumn('oauth_token', 'string', array(
'length' => 255,
'notnull' => true));
$table->addColumn('oauth_shard_id', 'string', array(
'length' => 4,
'notnull' => true,
'fixed' => true));

$table->setPrimaryKey(array('customer_id'), 'pk_customer_id');
$table->addForeignKeyConstraint($schema->getTable('customer'), array('customer_id'), array('id'));
}

public function down(Schema $schema)
{
$schema->dropTable('customer_evernote');
}

public function preUp(Schema $schema)
{
$this->addSql("ALTER TABLE `customer` ENGINE = INNODB");
}

public function postUp(Schema $schema)
{
$this->skipIf($this->version->isMigrated() !== true, 'postUp can only apply if migration completes.');

// Copy the data from the customer table into the newly created customer_evernote table.
$this->doctrine = \Zend_Registry::get('doctrine');
$this->entityManager = $this->doctrine->getEntityManager();
$this->customerRepository = $this->entityManager->getRepository('My\Entity\Customer');
$this->evernoteRepository = $this->entityManager->getRepository('My\Entity\CustomerEvernote');
$customers = $this->customerRepository->findAll();

foreach ($customers as $customer)
{
$evernoteRecord = new \My\Entity\CustomerEvernote();
$evernoteRecord->setCustomerId($customer->getId());
$evernoteRecord->setCustomer($customer);
$evernoteRecord->setOauthToken($customer->getEvernoteOauthToken());
$evernoteRecord->setOauthShardId($customer->getEvernoteOauthShardId());
$evernoteRecord->setIntegrationDate(new \DateTime("now"));

$this->evernoteRepository->saveEvernote($evernoteRecord);
}

// Drop the columns from the existing customer table.
$table = $schema->getTable('customer');
$table->dropColumn('evernote_oauth_token');
$table->dropColumn('evernote_oauth_shard_id');
}

public function preDown(Schema $schema)
{
// Create the existing columns in the customer table.
$table = $schema->getTable('customer');
$table->addColumn('evernote_oauth_token', 'string', array(
'length' => 255,
'notnull' => false));
$table->addColumn('evernote_oauth_shard_id', 'string', array(
'length' => 4,
'notnull' => false,
'fixed' => true));

// Copy the data to the customer table.
$this->doctrine = \Zend_Registry::get('doctrine');
$this->entityManager = $this->doctrine->getEntityManager();
$this->customerRepository = $this->entityManager->getRepository('My\Entity\Customer');
$this->evernoteRepository = $this->entityManager->getRepository('My\Entity\CustomerEvernote');

$integrations = $this->evernoteRepository->findAll();

foreach ($integrations as $integration)
{
$integration->getCustomer()->setEvernoteOauthToken($integration->getOauthToken());
$integration->getCustomer()->setEvernoteOauthShardId($integration->getOauthShardId());

$this->customerRepository->saveCustomer($integration->getCustomer());
}
}
}

实际上,如果我将 postUp() 末尾的代码移动到新版本的 up() 方法,并将 preDown() 开头的代码移动到新版本的 down() 方法,我会得到与上面的类中的结果相同,只是分两个单独的步骤执行。通过这种方法,我保证在我的 up 和 down 方法中严格进行结构修改。

关于doctrine - 删除 postUp() 方法中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5564945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com