Migration.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use common\helpers\ArrayHelper;
  5. /**
  6. * Class Migration
  7. * @package common\components
  8. */
  9. class Migration extends \yii\db\Migration
  10. {
  11. /**
  12. * 修改字段名称
  13. *
  14. * @param string $table
  15. * @param string $column
  16. * @param string $type
  17. * @throws \yii\db\Exception
  18. */
  19. public function renameColumn($table, $name, $newName)
  20. {
  21. if (!empty($data = Yii::$app->db->createCommand("SHOW COLUMNS FROM $table LIKE '$name'")->queryAll())) {
  22. parent::renameColumn($table, $name, $newName);
  23. }
  24. }
  25. /**
  26. * 修改字段类型
  27. *
  28. * @param string $table
  29. * @param string $column
  30. * @param string $type
  31. * @throws \yii\db\Exception
  32. */
  33. public function alterColumn($table, $column, $type)
  34. {
  35. if (!empty($data = Yii::$app->db->createCommand("SHOW COLUMNS FROM $table LIKE '$column'")->queryAll())) {
  36. parent::alterColumn($table, $column, $type);
  37. }
  38. }
  39. /**
  40. * 添加字段
  41. *
  42. * @param string $table
  43. * @param string $column
  44. * @param string $type
  45. */
  46. public function addColumn($table, $column, $type)
  47. {
  48. if (empty($data = Yii::$app->db->createCommand("SHOW COLUMNS FROM $table LIKE '$column'")->queryAll())) {
  49. parent::addColumn($table, $column, $type);
  50. }
  51. }
  52. /**
  53. * 创建表
  54. *
  55. * @param string $table
  56. * @param array $columns
  57. * @param null $options
  58. */
  59. public function createTable($table, $columns, $options = null)
  60. {
  61. $tmpTable = Yii::$app->db->getSchema()->getRawTableName($table);
  62. if (empty($data = Yii::$app->db->createCommand("SHOW TABLES LIKE '$tmpTable';")->queryAll())) {
  63. parent::createTable($table, $columns, $options);
  64. }
  65. }
  66. /**
  67. * 修改表名称
  68. *
  69. * @param string $table
  70. * @param string $newName
  71. * @throws \yii\base\NotSupportedException
  72. * @throws \yii\db\Exception
  73. */
  74. public function renameTable($table, $newName)
  75. {
  76. $tmpTable = Yii::$app->db->getSchema()->getRawTableName($table);
  77. $tmpNewName = Yii::$app->db->getSchema()->getRawTableName($newName);
  78. if (
  79. !empty($data = Yii::$app->db->createCommand("SHOW TABLES LIKE '$tmpTable';")->queryAll()) &&
  80. empty($dataNew = Yii::$app->db->createCommand("SHOW TABLES LIKE '$tmpNewName';")->queryAll())
  81. ) {
  82. parent::renameTable($table, $newName);
  83. }
  84. }
  85. /**
  86. * 创建索引
  87. *
  88. * @param string $name
  89. * @param string $table
  90. * @param array|string $columns
  91. * @param false $unique
  92. * @throws \yii\base\NotSupportedException
  93. * @throws \yii\db\Exception
  94. */
  95. public function createIndex($name, $table, $columns, $unique = false)
  96. {
  97. $tmpTable = Yii::$app->db->getSchema()->getRawTableName($table);
  98. $data = Yii::$app->db->createCommand("SHOW INDEX FROM $tmpTable")->queryAll();
  99. $columnName = ArrayHelper::getColumn($data, 'Column_name');
  100. $tmpColumns = explode(',', $columns);
  101. $status = false;
  102. foreach ($tmpColumns as $column) {
  103. if (in_array($column, $columnName)) {
  104. $status = true;
  105. }
  106. }
  107. if ($status === false) {
  108. parent::createIndex($name, $table, $columns, $unique);
  109. }
  110. }
  111. }
粤ICP备19079148号