ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

PHP|ThinkPHP|Yii 数据库迁移(Phinx)的使用

2021-10-13 14:04:13  阅读:263  来源: 互联网

标签:COMMENT dbConfig 数据库 Yii prefix hostport ThinkPHP table PHP


        首先,PHP Composer, ThinkPHP,Yii 这三者的数据库迁移都是基于Phinx来操作。而对于基础数据库的建表可以导出一份SQL文件作为基础数据也不需要在写create table什么的比较费时间。

        讲解场景:当你又一个sell数据库,其中有表sell,你要新增tille_2字段,其中sell表基础SQL语句是:

-- Adminer 4.3.1 MySQL dump

SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `wm_abouts`;
CREATE TABLE `wm_abouts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `skey` varchar(100) NOT NULL DEFAULT '0' COMMENT '标识',
  `title` varchar(100) DEFAULT '' COMMENT '标题',
  `content` text COMMENT '内容',
  `video` varchar(255) DEFAULT NULL,
  `w_time` int(10) unsigned NOT NULL COMMENT '创建时间',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态(0隐藏,1显示)',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='单页管理';


-- 2021-10-13 05:47:22
  • 初始化:
php7.2 vendor/bin/phinx init

         生成了一个配置文件[phinx.php]:

<?php
$dbConfig = require '../app/database.php';

return
[
    'paths' => [
        'migrations' => '%%PHINX_CONFIG_DIR%%/db/migrations', // 文件夹
        'seeds' => '%%PHINX_CONFIG_DIR%%/db/seeds' // 文件夹
    ],
    'environments' => [
        'default_migration_table' => 'phinxlog', // 生成一个表记录日志
        'default_environment' => 'development', // 运行下列那个数据库环境配置
        'production' => [
            'adapter' => 'mysql', // 数据库类型
            'host' => $dbConfig['hostname'], // 数据库地址
            'name' => $dbConfig['database'], // 数据库名
            'user' => $dbConfig['username'], // 数据库连接账号
            'pass' => $dbConfig['password'], // 数据库连接密码
            'port' => $dbConfig['hostport'], // 数据库端口号
            'table_prefix' => $dbConfig['hostport']['prefix'], // 数据库前缀
            'charset' => 'utf8', // 数据库字符集
        ],
        'development' => [
            'adapter' => 'mysql',
            'host' => $dbConfig['hostname'],
            'name' => $dbConfig['database'],
            'user' => $dbConfig['username'],
            'pass' => $dbConfig['password'],
            'port' => $dbConfig['hostport'],
            'table_prefix' => $dbConfig['hostport']['prefix'],
            'charset' => 'utf8',
        ],
        'testing' => [
            'adapter' => 'mysql',
            'host' => $dbConfig['hostname'],
            'name' => $dbConfig['database'],
            'user' => $dbConfig['username'],
            'pass' => $dbConfig['password'],
            'port' => $dbConfig['hostport'],
            'table_prefix' => $dbConfig['hostport']['prefix'],
            'charset' => 'utf8',
        ]
    ],
    'version_order' => 'creation'
];
  • 成一个版本文件:
 php7.2 vendor/bin/phinx create ChangeTableAbout

        默认代码如下:

<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Abouts extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {

    }
}

注意:需要你自己填充代码到change:

<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class Abouts extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {


        // 添加字段title_2
        $table = $this->table('abouts');
        $table->addColumn('title_2', 'string', array(
            'limit' => 100, 'default' => 'default_title2', 'comment' => '副标题'
        ))->save();
    }
}

 运行:php7.2 phinx migrate --dry-run

标签:COMMENT,dbConfig,数据库,Yii,prefix,hostport,ThinkPHP,table,PHP
来源: https://blog.csdn.net/wpj130/article/details/120742165

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有