adhuham / pretty-phinx
Phinx,但更加美观
Requires
- php: >=7.2
- robmorgan/phinx: ^0.12.6
This package is auto-updated.
Last update: 2024-09-10 14:05:52 UTC
README
Pretty Phinx 为流行的 Phinx 迁移库添加了美观和可读的语法。Phinx 的默认语法有时可能会变得杂乱且难以阅读。Pretty Phinx 使用方法链并为流行的列类型添加了可读方法。
安装
使用 composer
composer require adhuham/pretty-phinx
控制台
使用 vendor/bin/pretty-phinx
代替 vendor/bin/phinx
来访问控制台工具。
迁移
你需要在迁移类中扩展 PrettyPhinx\Migration\AbstractMigration
而不是 Phinx\Migration\AbstractMigration
基类。当你使用 vendor/bin/pretty-phinx
生成迁移类时,它将使用 vendor/bin/pretty-phinx
创建这些类。
use PrettyPhinx\Migration\AbstractMigration; class MyNewMigration extends AbstractMigration { public function change() { } }
创建表
$post = $this->table('post'); $post->bigIncrements('id')->add(); $post->string('slug')->unique()->add(); $post->string('title')->length(255)->add(); $post->text('content')->nullable()->add(); $post->create();
更新表
$post = $this->table('post'); $post->bigIncrements('id')->change(); $post->string('slug')->unique()->change(); $post->string('title')->length(255)->nullable()->change(); $post->text('content')->nullable()->change(); $post->string('sub_title')->after('title'); $post->update();
使用 ->change()
代替 ->add()
来修改列。
主键
默认情况下,自动创建主键的功能是禁用的。你需要手动使用 自增方法 来创建主键。
外键
$tag = $this->table('tag'); $tag->bigIncrements()->add(); $tag->string('slug')->unique()->add(); $tag->string('name')->add(); $tag->create(); $post = $this->table('post'); $post->unsignedBigInteger('tag_id'); $post->foreign('tag_id')->references('id')->on('tag') ->onUpdate('cascade') ->onDelete('cascade') ->add(); $post->update();
使用 Phinx 的默认语法来删除或检查外键的存在性($table->dropForeignKey('tag_id')
和 $table->hasForeignKey('tag_id')
)。参见 Phinx 文档:处理外键
方法
$table->unsignedBigInteger(); $table->unsignedMediumInteger(); $table->unsignedSmallInteger(); $table->unsignedTinyInteger(); $table->unsignedInteger(); $table->bigInteger(); $table->mediumInteger(); $table->smallInteger(); $table->tinyInteger(); $table->integer(); $table->text(); $table->longText(); $table->mediumText(); $table->tinyText(); $table->decimal($precision = 8, $scale = 2); $table->boolean(); $table->dateTime(); $table->date(); $table->time();
自增
// these methods will create an auto-incrementing column $table->bigIncrements(); $table->mediumIncrements(); $table->smallIncrements(); $table->tinyIncrements(); $table->increments();
修饰符
// ->length() $table->string('title')->length(100)->add(); // ->default() $table->string('title')->default('untitled')->add(); // ->nullable() $table->string('title')->nullable()->add(); $table->string('title')->nullable(false)->add(); // disable null (NOT NULL) // ->unique() $table->string('title')->unique()->add(); // ->index() $table->string('title')->index()->add(); // ->after() $table->string('title')->after('another_column')->add(); // ->charset() $table->string('title')->charset('utf8')->add(); // ->collation() $table->string('title')->collation('utf8_general_ci')->add();
编码
默认字符集/校对为表和列都是 utf8mb4
和 utf8mb4_unicode_ci
。
更改表的校对
$table = $this->table('post'); // this changes the collation of "post" table $table->collation('utf8_general_ci'); // ... // ... $table->update();
许可证
MIT 许可证
版权所有 (c) 2021 Mohamed Adhuham
特此授予任何人免费获得本软件及其相关文档副本(“软件”)的副本的权限,用于在不受限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件的副本,并允许获得软件的人这样做,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,没有任何形式的保证,明示或暗示,包括但不限于对适销性、特定用途适用性和非侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是在合同行为、侵权行为或其他行为中,是由软件本身引起的,还是与使用或操作软件有关。