工具目录
见 yiisql
目录
如果熟悉 php
可以用 yiisql
管理 mysql
的建表、表结构的修改。
安装 php
1.版本需要 >= 7.4.0。
2.下载地址
3.安装完后将 php
的安装目录的路径加到环境变量。
安装 composer
-
修改镜像地址
composer config -g repo.packagist composer https://packagist.phpcomposer.com
安装 Yii
1.官网地址
2.命令
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
主要配置和目录
1.目录YiiRootDirectory/migration
|--YiiRootDirectory
|--config
|-- db.php
|-- migration.php
|-- console.php
|--migration
|--center -------- 中心数据库的表格(迁移记录)
|--game -------- 游戏数据库的表格(迁移记录)
2.yiisql/config/db.php
<?php
return [
'components' => [
'dbGame' => [ // 游戏数据库
'class' => yii\db\Connection::class,
'dsn' => 'mysql:host=localhost;dbname=fd_game',
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
// Schema cache options (for production environment)
//'enableSchemaCache' => true,
//'schemaCacheDuration' => 60,
//'schemaCache' => 'cache',
],
'dbCenter' => [ // 中心数据库
'class' => yii\db\Connection::class,
'dsn' => 'mysql:host=localhost;dbname=fd_center',
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
// Schema cache options (for production environment)
//'enableSchemaCache' => true,
//'schemaCacheDuration' => 60,
//'schemaCache' => 'cache',
],
],
];
3.迁移配置 migration.php
<?php
// https://www.yiichina.com/doc/guide/2.0/db-migrations
return [
'controllerMap' => [
'migrate-center' => [ // 组件名称
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => ['app\migration\center'], // 迁移类文件的命名空间
'migrationTable' => 'migration', // 迁移表的名字
'migrationPath' => null, // 使用了命名空间,这里为 null
'db' => 'dbCenter' // 数据库连接, 见上面 db.php
],
'migrate-game' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => ['app\migration\game'],
'migrationTable' => 'migration',
'migrationPath' => null,
'db' => 'dbGame'
]
],
];
console.php
配置
<?php
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$migration = require __DIR__ . '/migration.php';
$config = [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\commands',
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
'@tests' => '@app/tests',
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
$config = \yii\helpers\ArrayHelper::merge($config, $db, $migration);
return $config;
命令操作
1.终端打开 yiisql
目录。
2.具体命令见 Yii Migration。
3.举例
-- 中心服创建迁移
php yii migrate-center/create create_acccount
-- 中心服提交迁移
php yii migrate-center
-- 中心服还原前一条迁移
php yii migrate-center/down 1
-- 游戏服创建迁移
php yii migrate-game/create create_actor
-- 游戏服提交迁移
php yii migrate-game
-- 游戏服还原前一条迁移
php yii migrate-game/down 1
center
服的表 create_bulletin
迁移代码
<?php
namespace app\migration\center;
use yii\db\Migration;
/**
* Class M210421030007CreateBulletin
*/
class M210421030007CreateBulletin extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$table_name = 'bulletin';
$this->createTable($table_name, [
'id' => $this->integer(),
]);
$this->addPrimaryKey('id', 'bulletin', 'id');
$this->addColumn($table_name, "bulletins", "blob");
$this->addCommentOnColumn($table_name, "bulletins", "公告内容");
$this->execute("alter table {$table_name} add column `create_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP");
$this->execute("alter table {$table_name} add column `update_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0)");
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('bulletin');
return true;
}
}