Skip to content

Commit 887df36

Browse files
committed
Updates writing-migrations.md
Auto commit by GitBook Editor
1 parent cb96e70 commit 887df36

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

writing-migrations.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ $ php vendor/bin/phinx create MyNewMigration
1616

1717
如果你指定了多个脚本路径,将会提示你选择哪一个。
1818

19-
20-
2119
Phinx 自动创建的迁移脚本框架有一个方法:
2220

2321
```
@@ -55,5 +53,61 @@ class MyNewMigration extends AbstractMigration
5553
}
5654
```
5755

56+
所有迁移脚本都继承类 `AbstractMigration` 。这个类提供了迁移脚本的基本方法。迁移脚本可以通过很多方法改变你的数据库,比如创建新表、插入数据、增加索引和修改字段等等。
57+
58+
59+
60+
### Change 方法
61+
62+
Phinx 0.2.0 介绍了一个新功能-逆迁移(回滚)。现在这个功能成为了脚本的默认方法。在这个方法中,你只需要定义 `up` 的逻辑,Phinx 可以在回滚的时候自动识别出如何down。比如:
63+
64+
```
65+
<?php
66+
67+
use Phinx\Migration\AbstractMigration;
68+
69+
class CreateUserLoginsTable extends AbstractMigration
70+
{
71+
/**
72+
* Change Method.
73+
*
74+
* More information on this method is available here:
75+
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
76+
*
77+
* Uncomment this method if you would like to use it.
78+
*/
79+
public function change()
80+
{
81+
// create the table
82+
$table = $this->table('user_logins');
83+
$table->addColumn('user_id', 'integer')
84+
->addColumn('created', 'datetime')
85+
->create();
86+
}
87+
88+
/**
89+
* Migrate Up.
90+
*/
91+
public function up()
92+
{
93+
94+
}
95+
96+
/**
97+
* Migrate Down.
98+
*/
99+
public function down()
100+
{
101+
102+
}
103+
}
104+
```
105+
106+
当执行这个迁移脚本,Phinx 将创建 `user_logins` 表,并且在回滚的时候自动删除该表。注意,当 `change` 方法存在的时候,`up``down` 方法会被自动忽略。如果你想用这些方法建议你创建另外一个迁移脚本。
107+
108+
> 当在 `change()` 方法中创建或者更新表的时候你必须使用 `create()` 或者 `update()` 方法。当使用 `save()` 方法时,Phinx无法识别是创建还是修改数据表。
109+
110+
111+
58112

59113

0 commit comments

Comments
 (0)