gpt4 book ai didi

php - Phinx 迁移 'up' 与 'change' 函数

转载 作者:行者123 更新时间:2023-12-02 16:58:26 26 4
gpt4 key购买 nike

我已经使用了phinx迁移并使用了它的“向上”和“更改”功能,但我没有注意到它们之间有任何区别。下面是我的迁移文件。

    <?php

use Phinx\Migration\AbstractMigration;

class MediaManager extends AbstractMigration
{

public function up()
{
if($this->hasTable('uss_dish')){
$dish = $this->table('uss_dish');
$dish -> addColumn('coordinates','text',array('after' => 'district_id','default' => NULL))
-> save();
}
}

public function change()
{
if($this->hasTable('uss_dish')){
$dish = $this->table('uss_dish');
$dish -> addColumn('coordinates','text',array('after' => 'district_id','default' => NULL))
-> save();
}
}

}

谁能告诉我这两个函数之间的区别吗?提前致谢。

最佳答案

Phinx 0.2.0 添加了一项名为可逆迁移的新功能。您可以使用 change 方法实现可逆迁移。如果您已经实现了 change 方法,则实际上不需要编写 downup 方法。在向上迁移时,change 方法中的逻辑将被执行,Phinx 会为您找出如何自动向下迁移。

当您定义表结构等时,可逆迁移很有帮助。

示例(使用更改方法进行可逆迁移)

<?php

use Phinx\Migration\AbstractMigration;

class CreateUserLoginsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}

/**
* Migrate Up.
*/
public function up()
{

}

/**
* Migrate Down.
*/
public function down()
{

}
}

示例(与上面相同的迁移,但不使用更改方法)

<?php

use Phinx\Migration\AbstractMigration;

class CreateUserLoginsTable extends AbstractMigration
{


/**
* Migrate Up.
*/
public function up()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}

/**
* Migrate Down.
*/
public function down()
{
$this->dropTable('user_logins');
}
}

了解更多关于Reversible Migrastions Phinx的信息.

关于php - Phinx 迁移 'up' 与 'change' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48664174/

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