从 Yii2 模型重新创建数据库表

Recreate database tables from Yii2 models

我用 Yii2 编写了一个应用程序,不幸的是,我们丢失了所有数据库备份。我们现在拥有的只是应用程序文件。有没有更短的方法可以根据现有模型重新创建 98 个数据库表? 我注意到一些 22 表的架构被缓存在“/app/runtime/cache”下。有人做过这样的事吗?

您需要为每个模型创建迁移脚本以进行数据库备份。

您需要在迁移脚本 up() 函数中创建数据库架构,并且需要使用以下命令来管理数据库。

- migrate                      Manages application migrations.
    migrate/create             Creates a new migration.
    migrate/down               Downgrades the application by reverting old migrations.
    migrate/fresh              Truncates the whole database and starts the migration from the beginning.
    migrate/history            Displays the migration history.
    migrate/mark               Modifies the migration history to the specified version.
    migrate/new                Displays the un-applied new migrations.
    migrate/redo               Redoes the last few migrations.
    migrate/to                 Upgrades or downgrades till the specified version.
    migrate/up (default)       Upgrades the application by applying new migrations.

有关 Yii2 数据库迁移的更多信息,请参阅 Official Documentation

迁移脚本跟踪数据库,如果它再次丢失,您可以轻松创建它。

谢谢。

我必须警告我已经使用过这些,当您需要节省 $$ 时,它非常有用。

这是 Gii 的一个非常有趣的扩展,它至少可以帮助您重新启动数据库,然后您将使用它来修复一些问题。

它允许您执行的操作是从模型中的 PHPDoc 构建迁移。使用这些迁移来重建数据库。

您需要使用 composer 安装 https://github.com/Insolita/yii2-migrik,如果它给您带来一些麻烦,请使用版本 2.3 而不是版本 3。

添加

"insolita/yii2-migration-generator": "2.3"

然后打开Gii并使用"Model and PhpDoc migrations"。

现在使用 Yii2 迁移系统构建表,检查迁移,将它们与模型进行比较并添加关系,将它们 UP 并需要修复一些东西。这并不完美。但是可以节省时间。

https://www.yiiframework.com/doc/guide/2.0/en/db-migrations

祝你好运。