PHP code example of jcbowen / mysql-helper-yii2

1. Go to this page and download the library: Download jcbowen/mysql-helper-yii2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jcbowen / mysql-helper-yii2 example snippets


use Jcbowen\MysqlHelperYii2\components\MysqlHelper;

// 获取数据库名称
$dbName = MysqlHelper::getDBName();

// 获取完整表名(自动处理表前缀)
$tableName = MysqlHelper::tableName('user');

// 获取表结构
$schema = MysqlHelper::getTableSchema('user');

// 获取所有表名
$tables = MysqlHelper::getAllTables();

// 比较两个表结构
$diff = MysqlHelper::schemaCompare($table1Schema, $table2Schema);

// 生成修复 SQL
$fixSql = MysqlHelper::makeFixSql($currentSchema, $targetSchema, true);

// 获取数据库名称
MysqlHelper::getDBName(?string $dsn = '', string $db = 'db'): string

// 获取所有表名
MysqlHelper::getAllTables(string $db = 'db'): array

// 获取完整表名(处理前缀)
MysqlHelper::tableName(string $tableName, string $db = 'db'): string

// 获取表结构
MysqlHelper::getTableSchema(
    string $tableName = '', 
    bool $getDefault = true, 
    bool $getComment = true, 
    array $options = []
): array

// 获取数据库所有表的序列化结构
MysqlHelper::getTableSerialize(string $dbname = '', string $db = 'db'): string

// 根据结构生成建表语句
MysqlHelper::makeCreateSql(array $schema): string

// 根据表名生成删除表语句
MysqlHelper::makeDropSql(string $tableName): string

// 比较两张表的结构
MysqlHelper::schemaCompare(array $table1, array $table2): array

// 创建修复两张差异表的语句
MysqlHelper::makeFixSql(
    array $schema1, 
    array $schema2, 
    bool $strict = false
): array

// 获取指定表的 insert 语句
MysqlHelper::tableInsertSql(
    string $tableName, 
    array $options = [], 
    $db = null
): array|false

class MakeController extends \Jcbowen\MysqlHelperYii2\controllers\MakeController
{
    /**
     * 是否过滤没有数据模型的表
     */
    public $filterNoModelTable = false;
    
    /**
     * 不跟踪的表名
     */
    public $ignoreTables = ['migration', 'cache'];
    
    /**
     * 需要生成 insert 语句的表配置
     */
    public $insertTables = [
        'user' => [
            'truncate' => true, // 清空后插入
        ],
        'setting' => [
            'ignore' => true, // 存在就不插入
        ],
    ];
    
    /**
     * 数据库基准文件所在目录
     */
    public $dir = '@console/runtime/update/db';
    
    /**
     * 模型所在目录(支持多目录)
     */
    public $modelsDir = [
        '@common/models',      // 公共模型
        '@backend/models',     // 后台模型
        '@frontend/models',    // 前台模型
    ];
}

// 单目录配置(向后兼容)
public $modelsDir = '@common/models';

// 多目录配置(新功能)
public $modelsDir = [
    '@common/models',
    '@backend/models', 
    '@frontend/models',
    '@api/models',
];

// 生成当前数据库结构的基准文件
$controller = new MakeController();
$controller->actionDb();

// 检查结构变化
$controller->actionCheck();

// 配置多个模型目录
public $modelsDir = [
    '@common/models',      // 公共模型
    '@backend/models',     // 后台模型
    '@frontend/models',    // 前台模型
    '@api/models',         // API 模型
];

// 配置需要生成 insert 语句的表
public $insertTables = [
    'router' => [
        'truncate' => true, // 清空后插入
    ],
    'vip' => [
        'ignore' => true, // 存在就不插入
        'getInsertDataQuery' => function ($query) {
            return $query->where(['status' => 1]);
        }
    ],
];
bash
# 检查数据库表结构变化
php yii make/check

# 生成数据库基准文件
php yii make/db

# 使用非交互模式
php yii make/check --generate=y
php yii make/check --continues-if-empty=y
bash
# 生成数据库基准文件
php yii make/db