小智 25
在Yii中完成此操作的说明:
添加数据库连接设置protected/config/console.php
.
运行yiic migrate create initial
以创建迁移的存根代码.
将此要点的内容复制到protected/commands/InitialDbMigrationCommand.php
.
运行yiic initialdbmigration 'name_of_your_database' > initial_migration.php
以生成初始数据库迁移的方法up()
和down()
方法.
复制并粘贴up()
和down()
方法initial_migration.php
到protected/migrations
步骤2中文件夹中创建的文件.
由于迁移是关于设置数据库结构并对其进行更改,而不是反映当前数据库,因此没有这样的方法。
这也不是您必须执行的步骤。您可以从当前位置开始,这将使您能够回滚到这一点。这意味着您可以对当前表进行迁移,而无需指定其整个结构,而只需指定更改。
假设您有一个名为 user 的表,并且想要将他们的名字添加到其中。
php artisan migrate:make add_firstname_to_user
Run Code Online (Sandbox Code Playgroud)
现在进入application/migrations
并找到迁移文件,添加此
public function up()
{
Schema::table('user', function($table)
{
$table->string('firstname');
});
}
public function down() {
Schema::table('user', function($table)
{
$table->drop_column('firstname');
});
}
Run Code Online (Sandbox Code Playgroud)
现在您可以添加迁移它
php artisan migrate:install // if you haven't run this, should only be once
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
..并使用回滚
php artisan migrate:rollback
Run Code Online (Sandbox Code Playgroud)
这将添加或删除列名字,而不会以任何其他方式影响您的表。
归档时间: |
|
查看次数: |
12711 次 |
最近记录: |