DatabaseForm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace install\models;
  3. use yii\base\Model;
  4. use Yii;
  5. use yii\db\Connection;
  6. use yii\db\Exception;
  7. use common\helpers\Dsn;
  8. class DatabaseForm extends Model
  9. {
  10. public $username;
  11. public $password;
  12. public $database = 'yii2cmf';
  13. public $hostname = "127.0.0.1";
  14. public $port = "3306";
  15. public $prefix = "yii2cmf_";
  16. public function rules()
  17. {
  18. return [
  19. [['hostname', 'username', 'database', "hostname", "port"], 'required'],
  20. [['hostname'], 'checkDb'],
  21. [['password', "prefix"], 'safe']
  22. ];
  23. }
  24. public function checkDb($attribute, $params)
  25. {
  26. $dsn = "mysql:host=" . $this->hostname . ";dbname=" . $this->database.";port=".$this->port;
  27. // Create Test DB Connection
  28. Yii::$app->set('newDb', [
  29. 'class' => Connection::className(),
  30. 'dsn' => $dsn,
  31. 'username' => $this->username,
  32. 'password' => $this->password,
  33. 'charset' => 'utf8'
  34. ]);
  35. try {
  36. Yii::$app->get("newDb")->open();
  37. } catch (Exception $e) {
  38. switch ($e->getCode()) {
  39. case 1049:
  40. $this->addError("database", $e->getMessage());
  41. break;
  42. case 1045:
  43. $this->addError("password", $e->getMessage());
  44. break;
  45. case 2002:
  46. $this->addError("hostname", $e->getMessage());
  47. break;
  48. default:
  49. $this->addError("hostname", $e->getMessage());
  50. break;
  51. }
  52. }
  53. }
  54. public function loadDefaultValues()
  55. {
  56. $definitions = \Yii::$app->getComponents();
  57. if(isset($definitions["db"])&&isset($definitions["db"]['dsn']))
  58. {
  59. $dsn = Dsn::parse($definitions["db"]['dsn']);
  60. $this->hostname = $dsn->host;
  61. $this->database = $dsn->database;
  62. $this->port = $dsn->port;
  63. $this->username = $definitions["db"]['username'];
  64. $this->password = $definitions["db"]['password'];
  65. $this->prefix = $definitions["db"]['tablePrefix'];
  66. }
  67. }
  68. public function attributeLabels()
  69. {
  70. return [
  71. 'hostname' => '数据库地址',
  72. 'username' => '数据库用户名',
  73. 'password' => '数据库密码',
  74. 'database' => '数据库名称',
  75. "port" => "端口",
  76. "prefix" => "前缀"
  77. ];
  78. }
  79. public function save()
  80. {
  81. Yii::$app->setEnv('DB_USERNAME', $this->username);
  82. Yii::$app->setEnv('DB_PASSWORD', $this->password);
  83. Yii::$app->setEnv('DB_TABLE_PREFIX', $this->prefix);
  84. Yii::$app->setEnv('DB_DSN', "mysql:host=$this->hostname;dbname=$this->database;port=$this->port");
  85. Yii::$app->set('db', Yii::createObject([
  86. 'class' => 'yii\db\Connection',
  87. 'dsn' => "mysql:host=$this->hostname;dbname=$this->database;port=$this->port",
  88. 'username' => $this->username,
  89. 'password' => $this->password,
  90. 'charset' => 'utf8',
  91. "tablePrefix" => $this->prefix
  92. ])
  93. );
  94. return true;
  95. }
  96. }