M250825152436AddCommissionItemSettingDetail.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace addons\DoubleCopy\console\migrations;
  3. use yii\db\Migration;
  4. /**
  5. * 判断 allowance_prize 字段是否存在
  6. * Class M250825152436AddCommissionItemSettingDetail
  7. */
  8. class M250825152436AddCommissionItemSettingDetail extends Migration
  9. {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function safeUp()
  14. {
  15. try {
  16. // 要操作的表名
  17. $tableName = 'qimall_addons_double_copy_commission_item_setting_detail';
  18. // 要检查的字段名
  19. $columnName = 'allowance_price';
  20. if (!$this->columnExists($tableName, $columnName)) {
  21. $sql = <<<SQL
  22. ALTER TABLE `qimall_addons_double_copy_commission_item_setting_detail`
  23. ADD COLUMN `allowance_price` decimal(10, 2) NOT NULL COMMENT '津贴奖' AFTER `repurchase_prize`;
  24. SQL;
  25. $this->execute($sql);
  26. }
  27. } catch (\Exception $e) {
  28. }
  29. }
  30. protected function columnExists($tableName, $columnName): bool
  31. {
  32. $tableSchema = \Yii::$app->db->getTableSchema($tableName, true);
  33. return $tableSchema && in_array($columnName, $tableSchema->columnNames);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function safeDown()
  39. {
  40. echo "M250825152436AddCommissionItemSettingDetail cannot be reverted.\n";
  41. return false;
  42. }
  43. }