evopix / kohana-schema
这是Laravels Schema库到Kohana的移植。它提供了一种数据库无关的方式来操作表。它与 Doctrine DBAL 支持的所有数据库都兼容,并在这套系统中具有统一的API。
Requires
- php: >=5.4.0
- composer/installers: ~1.0
- doctrine/dbal: ~2.4.0
This package is not auto-updated.
Last update: 2020-06-12 17:44:19 UTC
README
简介
Kohana Schema
类提供了一种数据库无关的方式来操作表。它与 Kohana 支持的所有数据库都兼容,并在这套系统中具有统一的API。
创建与删除表
要创建一个新的数据库表,使用 Schema::create
方法
Schema::create('users', function($table)
{
$table->increments('id');
});
传递给 create
方法的第一个参数是表名,第二个是一个 Closure
,它将接收一个 Blueprint
对象,该对象可用于定义新表。
要重命名现有的数据库表,可以使用 rename
方法
Schema::rename($from, $to);
要指定模式操作应在哪个连接上进行,向方法传递第三个参数
Schema::create('users', function($table)
{
$table->increments('id');
}, 'default');
要删除表,可以使用 Schema::drop
方法
Schema::drop('users');
添加列
要更新现有的表,我们将使用 Schema::table
方法
Schema::table('users', function($table)
{
$table->string('email');
});
修改列
要修改现有表中的列,我们将使用 Schema::table
方法
Schema::table('users', function($table)
{
$table->modify_column('email', 'string', ['length' => '50']);
});
注意:SQLite 不支持修改列。
表构建器包含多种列类型,您可以在构建表时使用这些类型
命令 | 描述 |
---|---|
$table->big_increments('id'); |
使用“大整数”等效项进行ID递增。 |
$table->big_integer('votes'); |
与表相当的 BIGINT |
$table->binary('data'); |
与表相当的 BLOB |
$table->boolean('confirmed'); |
与表相当的 BOOLEAN |
$table->date('created_at'); |
与表相当的 DATE |
$table->datetime('created_at'); |
与表相当的 DATETIME |
$table->decimal('amount', 5, 2); |
具有精度和小数位的 DECIMAL 相当 |
$table->double('column', 15, 8); |
具有精度的 DOUBLE 相当 |
$table->enum('choices', array('foo', 'bar')); |
与表相当的 ENUM |
$table->float('amount'); |
与表相当的 FLOAT |
$table->increments('id'); |
将ID递增到表中(主键)。 |
$table->integer('votes'); |
与表相当的 INTEGER |
$table->long_text('description'); |
与表相当的 LONGTEXT |
$table->medium_text('description'); |
与表相当的 MEDIUMTEXT |
$table->morphs('taggable'); |
添加 INTEGER taggable_id 和 STRING taggable_type |
$table->small_integer('votes'); |
与表等价的小整数类型 |
$table->tiny_integer('numbers'); |
与表等价的小整数类型 |
$table->soft_deletes(); |
为软删除添加 deleted_at 列 |
$table->string('email'); |
与 VARCHAR 等价的列 |
$table->string('name', 100); |
具有长度的 VARCHAR 等价类型 |
$table->text('description'); |
与 TEXT 等价的类型 |
$table->time('sunrise'); |
与 TIME 等价的类型 |
$table->timestamp('added_on'); |
与 TIMESTAMP 等价的类型 |
$table->timestamps(); |
添加 created_at 和 updated_at 列 |
->nullable() |
指定该列允许 NULL 值 |
->default($value) |
声明列的默认值 |
->unsigned() |
将 INTEGER 设置为无符号 |
如果您正在使用 MySQL 数据库,可以使用 after
方法来指定列的顺序
在 MySQL 中使用 After
$table->string('name')->after('email');
重命名列
要重命名列,您可以在 Schema builder 上使用 rename_column
方法。在重命名列之前,请确保已将 doctrine/dbal
依赖项添加到您的 composer.json
文件中。
重命名列
Schema::table('users', function($table)
{
$table->rename_column('from', 'to');
});
注意:不支持重命名
enum
列类型。
删除列
从数据库表中删除列
Schema::table('users', function($table)
{
$table->drop_column('votes');
});
从数据库表中删除多个列
Schema::table('users', function($table)
{
$table->drop_column('votes', 'avatar', 'location');
});
添加索引
Schema builder 支持多种索引类型。有两种添加它们的方法。首先,您可以在列定义上流畅地定义它们,或者您可以单独添加它们
流畅地创建列和索引
$table->string('email')->unique();
或者,您可以选择在单独的行上添加索引。下面是所有可用索引类型列表
命令 | 描述 |
---|---|
$table->primary('id'); |
添加主键 |
$table->primary(array('first', 'last')); |
添加复合键 |
$table->unique('email'); |
添加唯一索引 |
$table->index('state'); |
添加基本索引 |
外键
Laravel 还提供了向表中添加外键约束的支持
向表中添加外键
$table->foreign('user_id')->references('id')->on('users');
在这个例子中,我们声明 user_id
列引用 users
表上的 id
列。
您还可以指定约束的 "on delete" 和 "on update" 操作的选项
$table->foreign('user_id')
->references('id')->on('users')
->on_delete('cascade');
要删除外键,您可以使用 drop_foreign
方法。外键的命名约定与其他索引类似
$table->drop_foreign('posts_user_id_foreign');
注意:在创建引用递增整数的外键时,请始终记住使外键列
unsigned
。
删除索引
要删除索引,必须指定索引的名称。Laravel 默认会为索引分配一个合理的名称。只需将表名、索引中列的名称和索引类型连接起来即可。以下是几个示例:
命令 | 描述 |
---|---|
$table->drop_primary('users_id_primary'); |
从 "users" 表中删除主键 |
$table->drop_unique('users_email_unique'); |
从 "users" 表中删除唯一索引 |
$table->drop_index('geo_state_index'); |
从 "geo" 表中删除基本索引 |
存储引擎
要设置表的存储引擎,请在架构构建器上设置 engine
属性。
Schema::create('users', function($table)
{
$table->engine = 'InnoDB';
$table->string('email');
});