Пример #1
0
 /**
  * Index is only called on fresh databases, when there are already settings
  * in database, the user will directly redirected to actionFinished()
  */
 public function actionIndex()
 {
     if (InstallerModule::checkDbConnection()) {
         $this->setupInitialData();
         return $this->redirect(Yii::$app->urlManager->createUrl('//installer/config/basic'));
     }
     return $this->redirect(Yii::$app->urlManager->createUrl('//installer/setup/database'));
 }
 public function actionDatabase()
 {
     $config = Configuration::get();
     $param = Configuration::getParam();
     $form = new DatabaseSettingForm();
     if ($form->load(Yii::$app->request->post())) {
         if ($form->validate()) {
             $dsn = "mysql:host=" . $form->hostname . ";dbname=" . $form->database;
             // Create Test DB Connection
             Yii::$app->set('db', ['class' => Connection::className(), 'dsn' => $dsn, 'username' => $form->username, 'password' => $form->password, 'charset' => 'utf8']);
             try {
                 Yii::$app->db->open();
                 // Check DB Connection
                 if (InstallerModule::checkDbConnection()) {
                     // Write Config
                     $config['components']['db']['class'] = Connection::className();
                     $config['components']['db']['dsn'] = $dsn;
                     $config['components']['db']['username'] = $form->username;
                     $config['components']['db']['password'] = $form->password;
                     $config['components']['db']['charset'] = 'utf8';
                     // Write config for future use
                     $param['installer']['db']['installer_hostname'] = $form->hostname;
                     $param['installer']['db']['installer_database'] = $form->database;
                     $param['installer']['db']['installer_username'] = $form->username;
                     Configuration::set($config);
                     Configuration::setParam($param);
                     Yii::$app->getSession()->setFlash('success', 'Database settings saved');
                 } else {
                     Yii::$app->getSession()->setFlash('danger', 'Incorrect configuration');
                 }
             } catch (Exception $e) {
                 Yii::$app->getSession()->setFlash('danger', $e->getMessage());
             }
         }
     } else {
         if (isset($param['installer']['db']['installer_hostname'])) {
             $form->hostname = $param['installer']['db']['installer_hostname'];
         }
         if (isset($param['installer']['db']['installer_database'])) {
             $form->database = $param['installer']['db']['installer_database'];
         }
         if (isset($param['installer']['db']['installer_username'])) {
             $form->username = $param['installer']['db']['installer_username'];
         }
     }
     return $this->render('database', ['model' => $form]);
 }
Пример #3
0
 /**
  * The init action imports the database structure & initial data
  */
 public function actionInit()
 {
     if (!InstallerModule::checkDbConnection()) {
         Yii::$app->db->open();
     }
     // Flush the caches
     if (Yii::$app->cache) {
         Yii::$app->cache->flush();
     }
     // todo make migration better
     $data = file_get_contents(dirname(__DIR__) . '/migrations/data.sql');
     Yii::$app->db->createCommand($data)->execute();
     return $this->redirect(Yii::$app->urlManager->createUrl('//installer/config/index'));
 }