Skip to content

Commit 963de2a

Browse files
committed
Updates writing-migrations.md
Auto commit by GitBook Editor
1 parent 305aa64 commit 963de2a

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

writing-migrations.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,123 @@ class MyNewMigration extends AbstractMigration
284284

285285
### Save 方法
286286

287+
当操作 Table 对象时,Phinx 提供了一些操作来改变数据库。
287288

289+
如果你不清楚该使用什么操作,建议你使用 save 方法。它将自动识别插入或者更新操作,并将改变应用到数据库。
290+
291+
### 创建一个表
292+
293+
使用 Table 可以很简单的创建一个表,现在我们创建一个存储用户信息的表
294+
295+
```
296+
<?php
297+
298+
use Phinx\Migration\AbstractMigration;
299+
300+
class MyNewMigration extends AbstractMigration
301+
{
302+
/**
303+
* Migrate Up.
304+
*/
305+
public function up()
306+
{
307+
$users = $this->table('users');
308+
$users->addColumn('username', 'string', array('limit' => 20))
309+
->addColumn('password', 'string', array('limit' => 40))
310+
->addColumn('password_salt', 'string', array('limit' => 40))
311+
->addColumn('email', 'string', array('limit' => 100))
312+
->addColumn('first_name', 'string', array('limit' => 30))
313+
->addColumn('last_name', 'string', array('limit' => 30))
314+
->addColumn('created', 'datetime')
315+
->addColumn('updated', 'datetime', array('null' => true))
316+
->addIndex(array('username', 'email'), array('unique' => true))
317+
->save();
318+
}
319+
320+
/**
321+
* Migrate Down.
322+
*/
323+
public function down()
324+
{
325+
326+
}
327+
}
328+
```
329+
330+
字段使用 `addColumn()` 方法创建。并且用 `addIndex()` 方法将 username 和 email 设置为唯一。最后调用 `save()` 提交我们的改变。
331+
332+
> Phinx 会为每个表自动创建一个自增的主键字段 `id`
333+
334+
id 选项会自动创建一个唯一字段,`primary_key`_ 选项设置哪个字段为主键。 `primary_key`_ 选项默认值是 `id` 。这2个选项可以设置为false。
335+
336+
如果要指定一个主键,你可以设置 `primary_key` 选项,关闭自动生成 `id` 选项,并使用2个字段定义为主键。
337+
338+
```
339+
<?php
340+
341+
use Phinx\Migration\AbstractMigration;
342+
343+
class MyNewMigration extends AbstractMigration
344+
{
345+
/**
346+
* Migrate Up.
347+
*/
348+
public function up()
349+
{
350+
$table = $this->table('followers', array('id' => false, 'primary_key' => array('user_id', 'follower_id')));
351+
$table->addColumn('user_id', 'integer')
352+
->addColumn('follower_id', 'integer')
353+
->addColumn('created', 'datetime')
354+
->save();
355+
}
356+
357+
/**
358+
* Migrate Down.
359+
*/
360+
public function down()
361+
{
362+
363+
}
364+
}
365+
```
366+
367+
单独设置 `primary_key`_ 选项并不能开启 `AUTO_INCREMENT`_ 选项。如果想简单的改变主键名,我们只有覆盖 `id` 字段名即可。
368+
369+
```
370+
<?php
371+
372+
use Phinx\Migration\AbstractMigration;
373+
374+
class MyNewMigration extends AbstractMigration
375+
{
376+
/**
377+
* Migrate Up.
378+
*/
379+
public function up()
380+
{
381+
$table = $this->table('followers', array('id' => 'user_id'));
382+
$table->addColumn('follower_id', 'integer')
383+
->addColumn('created', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))
384+
->save();
385+
}
386+
387+
/**
388+
* Migrate Down.
389+
*/
390+
public function down()
391+
{
392+
393+
}
394+
}
395+
```
396+
397+
另外,MySQL adapter 支持一下选项
398+
399+
| 选项 | 描述 |
400+
| :--- | :--- |
401+
| comment | 给表设置注释 |
402+
| engine | 定义表的引擎(默认 InnoDB) |
403+
| collation | 定义表的语言(默认 utf8-general-ci) |
288404

289405

290406

0 commit comments

Comments
 (0)