MigrateController.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace addons\RfDevTool\backend\controllers;
  3. use Yii;
  4. use common\helpers\FileHelper;
  5. use common\helpers\ArrayHelper;
  6. use addons\RfDevTool\common\models\MigrateForm;
  7. use jianyan\migration\components\MigrateCreate;
  8. /**
  9. * Class MigrateController
  10. * @package addons\RfDevTool\backend\controllers
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class MigrateController extends BaseController
  14. {
  15. /**
  16. * @return string
  17. * @throws \yii\base\InvalidConfigException
  18. * @throws \yii\db\Exception
  19. */
  20. public function actionIndex()
  21. {
  22. $model = new MigrateForm();
  23. // 表列表
  24. $tableList = array_map('array_change_key_case', Yii::$app->db->createCommand('SHOW TABLE STATUS')->queryAll());
  25. // 插件列表
  26. $addonList = Yii::$app->services->addons->findAll();
  27. if ($model->load(Yii::$app->request->post())) {
  28. if ($model->addon == "0") {
  29. $path = Yii::getAlias('@root') . '/console/migrations/';
  30. } else {
  31. $path = Yii::getAlias('@addons') . '/' . $model->addon . '/console/migrations/';
  32. FileHelper::mkdirs($path);
  33. }
  34. /** @var MigrateCreate $migrate */
  35. $migrate = Yii::createObject([
  36. 'class' => MigrateCreate::class,
  37. 'migrationPath' => $path
  38. ]);
  39. foreach ($model->tables as $table) {
  40. $migrate->create($table);
  41. }
  42. return $this->message('数据迁移创建成功', $this->redirect(['index']));
  43. }
  44. return $this->render($this->action->id, [
  45. 'tableList' => ArrayHelper::map($tableList, 'name', 'name'),
  46. 'addonList' => ArrayHelper::merge(['0' => '默认系统'], ArrayHelper::map($addonList, 'name', 'title')),
  47. 'model' => $model
  48. ]);
  49. }
  50. }
粤ICP备19079148号