thinkphp 迁移数据库 -Phinx 简单说明文档

php think 
migrate
  migrate:create     Create a new migration ///创建
  migrate:rollback   Rollback the last or to a specific migration //回滚
  migrate:run Migrate the database //执行 migrate:status Show migration status //状态查看 optimize optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded wit h classmaps too, good for production.//朗读优化PSR0和PSR4软件包,也可以通过类映射加载,有利于生产。 optimize:config Build config and common file cache.//构建公共配置文件缓存 optimize:route Build route cache.//构建路由缓存 optimize:schema Build database schema cache. //构建数据库构建缓存 seed seed:create Create a new database seeder //创建新的数据填充器 seed:run Run database seeders //运行填充器
#创建迁移类,首字母必须为大写
php think migrate:create Users

参考: http://docs.phinx.org/en/latest/index.html

注意:

  1.  Please be aware that when a change method exists, Phinx will automatically ignore the up and down methods. If you need to use these methods it is recommended to create a separate migration file.
    当change方法存在时,将会自动忽略up /down 方法,如果想要生效需要单独创建文件;
  2. When creating or updating tables inside a change() method you must use the Table create()and update() methods. Phinx cannot automatically determine whether a save() call is creating a new table or modifying an existing one.
    change()方法内创建或更新表时,必须使用表create()update()方法。Phinx无法自动确定save()是创建新表还是修改现有表。
  3. Phinx 只能撤销 createTable / renameTable / addColumn / renameColumn / addIndex / addForeignKey 命令;

—— Phinx支持在数据库表上创建外键约束。让我们在示例表中添加一个外键:

<?php

use Phinx\Migration\AbstractMigration;

class MyNewMigration extends AbstractMigration
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('tags');
        $table->addColumn('tag_name', 'string')
              ->save();

        $refTable = $this->table('tag_relationships');
        $refTable->addColumn('tag_id', 'integer', ['null' => true])
                 ->addForeignKey('tag_id', 'tags', 'id', ['delete'=> 'SET_NULL', 'update'=> 'NO_ACTION'])
                 ->save();

    }

    /**
     * Migrate Down.
     */
    public function down()
    {

    }
}

  

——有效列类型

In addition, the MySQL adapter supports enumset and blob column types.

In addition, the Postgres adapter supports jsonjsonbuuidcidrinet and macaddr column types (PostgreSQL 9.3 and above).

以下是有效的列选项:对于任何列类型:

 

For  decimal columns:

For enum and set columns:

For integer and biginteger columns:

For timestamp columns:

朗读您可以使用addTimestamps()方法将created_at和updated_at时间戳添加到表中。此方法还允许您提供替代名称。可选的第三个参数允许您更改要添加的列的时区选项。此外,您可以使用addTimestampsWithTimezone()方法,该方法是addTimestamps()的别名,它始终将第三个参数设置为true(请参阅下面的示例)。

<?php

use Phinx\Migration\AbstractMigration;

class MyNewMigration extends AbstractMigration
{
    /**
     * Migrate Change.
     */
    public function change()
    {
        // Use defaults (without timezones)
        $table = $this->table('users')->addTimestamps()->create();
        // Use defaults (with timezones)
        $table = $this->table('users')->addTimestampsWithTimezone()->create();

        // Override the 'created_at' column name with 'recorded_at'.
        $table = $this->table('books')->addTimestamps('recorded_at')->create();

        // Override the 'updated_at' column name with 'amended_at', preserving timezones.
        // The two lines below do the same, the second one is simply cleaner.
        $table = $this->table('books')->addTimestamps(null, 'amended_at', true)->create();
        $table = $this->table('users')->addTimestampsWithTimezone(null, 'amended_at')->create();
    }
}

  

For boolean columns:

For string and text columns:

For foreign key definitions:

Limit Option and MySQL

When using the MySQL adapter, additional hinting of database column type can be made for integertext and blob columns. Using limit with one the following options will modify the column type accordingly:

LimitColumn Type
BLOB_TINYTINYBLOB
BLOB_REGULARBLOB
BLOB_MEDIUMMEDIUMBLOB
BLOB_LONGLONGBLOB
TEXT_TINYTINYTEXT
TEXT_REGULARTEXT
TEXT_MEDIUMMEDIUMTEXT
TEXT_LONGLONGTEXT
INT_TINYTINYINT
INT_SMALLSMALLINT
INT_MEDIUMMEDIUMINT
INT_REGULARINT
INT_BIGBIGINT
use Phinx\Db\Adapter\MysqlAdapter;

//...

$table = $this->table('cart_items');
$table->addColumn('user_id', 'integer')
      ->addColumn('product_id', 'integer', ['limit' => MysqlAdapter::INT_BIG])
      ->addColumn('subtype_id', 'integer', ['limit' => MysqlAdapter::INT_SMALL])
      ->addColumn('quantity', 'integer', ['limit' => MysqlAdapter::INT_TINY])
      ->create();

  

Get a column list
$columns = $this->table('users')->getColumns();

Get a column by name
$column = $this->table('users')->getColumn('email');

Checking whether a column exists   检查列是否存在

<?php

use Phinx\Migration\AbstractMigration;

class MyNewMigration extends AbstractMigration
{
    /**
     * Change Method.
     */
    public function change()
    {
        $table = $this->table('user');
        $column = $table->hasColumn('username');

        if ($column) {
            // do something
        }

    }
}

 

  

更多查看:http://docs.phinx.org/en/latest/migrations.html#working-with-columns

转载于:https://www.cnblogs.com/q1104460935/p/10025645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThinkPHP 6 是一款基于PHP的开源框架,而 Uni-app 是一个跨平台的前端开发框架。您可以结合使用 ThinkPHP 6 和 Uni-app 来构建一个完整的前后端分离的应用。 以下是使用 ThinkPHP 6 和 Uni-app 的一般步骤: 1. 安装 ThinkPHP 6:您可以从 ThinkPHP 的官方网站(https://www.thinkphp.cn/)下载最新版本的 ThinkPHP 6,并按照官方文档进行安装和配置。 2. 创建 ThinkPHP 6 后端:使用 ThinkPHP 6 创建一个后端项目,您可以根据您的需求选择使用 MVC 或者其他开发模式。您可以定义路由、控制器、模型等来实现后端业务逻辑。 3. 创建 Uni-app 前端:使用 Uni-app 创建一个前端项目,Uni-app 可以一次编写,多端发布,可以生成小程序、H5、App 等多个平台的应用。您可以使用 Vue.js 的语法和组件来开发前端界面,并通过 API 调用与后端进行交互。 4. 定义 API 接口:在 ThinkPHP 6 后端中,您可以通过定义 API 接口来与 Uni-app 前端进行通信。您可以在控制器中编写接口方法,处理前端发送的请求,并返回相应的数据。 5. 前后端交互:在 Uni-app 前端中,您可以使用 Axios 或其他类似的 HTTP 客户端库来发送请求到后端的 API 接口,获取数据或提交数据。 6. 页面展示与交互:在 Uni-app 中,您可以使用 Vue.js 的语法和组件来定义页面的展示和交互逻辑,根据后端返回的数据来动态渲染页面,并处理用户的交互事件。 通过结合使用 ThinkPHP 6 和 Uni-app,您可以实现一个功能完善的前后端分离应用,提供良好的用户体验和高效的开发效率。具体的代码实现和细节,请参考 ThinkPHP 和 Uni-app 的官方文档和示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值