DatabaseConfigForm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace common\modules\config\models;
  3. use common\helpers\Dsn;
  4. use yii\base\Model;
  5. use yii\db\Connection;
  6. use yii\db\Exception;
  7. use Yii;
  8. class DatabaseConfigForm extends Model
  9. {
  10. use ConfigTrait;
  11. public $hostname;
  12. public $username;
  13. public $password;
  14. public $database;
  15. public function rules()
  16. {
  17. return [
  18. [['hostname', 'username', 'database'], 'required'],
  19. [['hostname', 'username', 'database', 'password'], 'checkDb']
  20. ];
  21. }
  22. public function checkDb($attribute, $params)
  23. {
  24. $dsn = "mysql:host=" . $this->hostname . ";dbname=" . $this->database;
  25. // Create Test DB Connection
  26. Yii::$app->set('newDb', [
  27. 'class' => Connection::className(),
  28. 'dsn' => $dsn,
  29. 'username' => $this->username,
  30. 'password' => $this->password,
  31. 'charset' => 'utf8'
  32. ]);
  33. try {
  34. Yii::$app->get("newDb")->open();
  35. } catch (Exception $e) {
  36. switch ($e->getCode()) {
  37. case 1049:
  38. $this->addError("database", $e->getMessage());
  39. break;
  40. case 1045:
  41. $this->addError("password", $e->getMessage());
  42. break;
  43. case 2002:
  44. $this->addError("hostname", $e->getMessage());
  45. break;
  46. default:
  47. $this->addError("hostname", $e->getMessage());
  48. break;
  49. }
  50. }
  51. }
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'hostname' => '数据库地址',
  56. 'username' => '数据库用户名',
  57. 'password' => '数据库密码',
  58. 'database' => '数据库名字'
  59. ];
  60. }
  61. public function loadDefaultValues()
  62. {
  63. $db = \Yii::$app->getDb();
  64. $dsn = Dsn::parse($db->dsn);
  65. $this->hostname = $dsn->host;
  66. $this->database = $dsn->database;
  67. $this->username = $db->username;
  68. $this->password = $db->password;
  69. }
  70. public function save($runValidation = true, $attributeNames = null)
  71. {
  72. if ($runValidation && ! $this->validate($attributeNames)) {
  73. return false;
  74. }
  75. $config = $this->getConfig();
  76. $config->set('DB_DSN', "mysql:host=" . $this->hostname . ";dbname=" . $this->database.";port=3306");
  77. $config->set('DB_USERNAME', $this->username);
  78. $config->set('DB_PASSWORD', $this->password);
  79. return true;
  80. }
  81. // 不用env的用这个方法
  82. public function save2($runValidation = true, $attributeNames = null)
  83. {
  84. if ($runValidation && ! $this->validate($attributeNames)) {
  85. return false;
  86. }
  87. $config = $this->getConfig();
  88. $db = [];
  89. $db['class'] = 'yii\db\Connection';
  90. $db['dsn'] = "mysql:host=" . $this->hostname . ";dbname=" . $this->database.";port=3306";
  91. $db['username'] = $this->username;
  92. $db['password'] = $this->password;
  93. $localConfig = $config->getConfigFromLocal();
  94. $localConfig['components']['db'] = $db;
  95. $config->setConfigToLocal($localConfig);
  96. Yii::$app->set('db', $db);
  97. return true;
  98. }
  99. }