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

时间:2018-06-15 15:25:30

标签: mysql yii2 yii2-basic-app yii2-model

我在Yii2中写了一个应用程序,不幸的是,我们丢失了所有的数据库备份。我们现在只有应用程序文件。是否有基于现有模型重建98数据库表的更短方法? 我注意到有22个桌子'架构缓存在" / app / runtime / cache"下。有人做过这样的事吗?

2 个答案:

答案 0 :(得分:4)

我必须警告我已经使用了这些并且它非常有用,当你需要一些$$储蓄时。

对于Gii来说,这是一个非常有趣的扩展,至少可以帮助你重新启动数据库,然后你将继续努力解决一些问题。

它允许您做什么,它是从模型中的PHPDoc构建迁移。使用这些迁移来重建数据库。

您需要使用编辑器安装https://github.com/Insolita/yii2-migrik,如果它在使用2.3版而不是3版时遇到麻烦。

添加

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

然后打开Gii并使用"模型和PhpDoc迁移"。

  1. Model and PhpDoc migrations

  2. Config

    1. Generated file

    2. 现在使用Yii2迁移系统来构建表,检查迁移,将它们与模型进行比较并添加关系,将它们UP起来并需要修复一些内容。它并不完美。但它节省了时间。

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

      祝你好运。

答案 1 :(得分:0)

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

您需要在迁移脚本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.

有关更多信息,请参阅embed vlc player in HTML了解Yii2数据库迁移。

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

感谢。