@@ -20,14 +20,14 @@ public function init()
20
20
parent ::init ();
21
21
}
22
22
23
- public $ _migrateLog = '' ;
23
+ // public $_migrateLog = '';
24
24
25
25
// 安装默认第一步页面
26
26
public function actionIndex ()
27
27
{
28
28
$ editForm = Yii::$ app ->request ->post ('editForm ' );
29
29
if ($ editForm && $ this ->checkDatabaseData ($ editForm )
30
- && $ this ->updateDatabase ($ editForm )) {
30
+ && $ this ->updateDatabaseConfig ($ editForm )) {
31
31
Yii::$ app ->session ->setFlash ('database-success ' , 'mysql config set success, mysql config file path: @common/config/main-local.php ' );
32
32
// 进行跳转
33
33
$ homeUrl = Yii::$ app ->homeUrl ;
@@ -44,6 +44,25 @@ public function actionIndex()
44
44
// 数据库migrate页面
45
45
public function actionMigrate ()
46
46
{
47
+
48
+ $ isPost = Yii::$ app ->request ->post ('isPost ' );
49
+ if ($ isPost ) {
50
+ // 进行数据库初始化
51
+ if ($ this ->runMigrate ()) {
52
+ $ successInfo = '数据库migrate初始化完成 ' ;
53
+ $ successInfo = $ this ->getSuccessHtml ($ successInfo );
54
+ //exit;
55
+ return $ this ->render ('migratesuccess ' , [
56
+ 'successInfo ' => $ successInfo ,
57
+ 'nextUrl ' => Yii::$ app ->homeUrl . '/database/config/addtestdata ' ,
58
+ 'skipUrl ' => Yii::$ app ->homeUrl . '/database/config/complete ' ,
59
+ ]);
60
+ } else {
61
+ $ errors = 'migrate 失败,你可以在logs文件中查看具体原因(@appfront/config/main.php中log组件,对应的logFile配置,查看该log文件,如果没有可以手动创建该log文件,清空数据库,重新执行该操作) ' ;
62
+ Yii::$ app ->session ->setFlash ('migrate-errors ' , $ errors );
63
+ }
64
+ }
65
+
47
66
$ successInfo = Yii::$ app ->session ->getFlash ('database-success ' );
48
67
$ successInfo = $ this ->getSuccessHtml ($ successInfo );
49
68
$ errorInfo = Yii::$ app ->session ->getFlash ('migrate-errors ' );
@@ -52,27 +71,29 @@ public function actionMigrate()
52
71
return $ this ->render ($ this ->action ->id , [
53
72
'successInfo ' => $ successInfo ,
54
73
'errorInfo ' => $ errorInfo ,
55
- 'initUrl ' => Yii::$ app ->homeUrl . '/database/config/migrateprocess ' ,
56
- 'nextUrl ' => Yii::$ app ->homeUrl . '/database/config/addtestdata ' ,
57
- 'migrateLog ' => $ this ->_migrateLog
58
74
]);
59
75
}
60
-
76
+ // 产品测试数据添加
61
77
public function actionAddtestdata ()
62
78
{
63
- $ errorInfo = Yii::$ app ->session ->getFlash ('add-test-errors ' );
64
- $ errorInfo = $ this ->getErrorHtml ($ errorInfo );
79
+ if ($ this ->addProductData ()) {
80
+ $ successInfo = $ this ->getSuccessHtml ('产品测试数据添加成功 ' );
81
+ } else {
82
+ $ errorInfo = Yii::$ app ->session ->getFlash ('add-test-data-errors ' );
83
+ $ errorInfo = $ this ->getErrorHtml ($ errorInfo );
84
+ }
65
85
66
86
return $ this ->render ($ this ->action ->id , [
67
87
'errorInfo ' => $ errorInfo ,
88
+ 'successInfo ' => $ successInfo ,
68
89
'initUrl ' => Yii::$ app ->homeUrl . '/database/config/addtestdatainit ' ,
69
90
'nextUrl ' => Yii::$ app ->homeUrl . '/database/config/complete ' ,
70
91
//'migrateLog' => $this->_migrateLog
71
92
]);
72
93
73
94
}
74
95
// 进行sql migrate ,产品图片的复制
75
- public function actionAddtestdatainit ()
96
+ public function addProductData ()
76
97
{
77
98
// 1. 图片的复制
78
99
$ sourcePath = dirname (Yii::getAlias ('@common ' )) . '/environments/test_data/appimage ' ;
@@ -87,34 +108,15 @@ public function actionAddtestdatainit()
87
108
try {
88
109
$ result = $ conn ->createCommand ($ sqlStr )->execute ();
89
110
$ innerTransaction ->commit ();
90
- echo json_encode ([
91
- 'status ' => 'success ' ,
92
- ]);
93
111
94
- exit ;
112
+ return true ;
95
113
} catch (\Exception $ e ) {
96
114
$ innerTransaction ->rollBack ();
97
115
$ message = $ e ->getMessage ();
98
- echo json_encode ([
99
- 'status ' => 'fail ' ,
100
- 'info ' => $ message ,
101
- ]);
102
-
103
- exit ;
116
+ Yii::$ app ->session ->setFlash ('add-test-data-errors ' , $ message );
104
117
}
105
- echo json_encode ([
106
- 'status ' => 'fail ' ,
107
- 'info ' => 'error ' ,
108
- ]);
109
118
110
- exit ;
111
- }
112
-
113
- // 进行数据库的migrate操作(ajax)
114
- public function actionMigrateprocess ()
115
- {
116
- $ this ->runMigrate ();
117
- exit ;
119
+ return false ;
118
120
}
119
121
120
122
// 完成页面
@@ -124,7 +126,7 @@ public function actionComplete()
124
126
}
125
127
126
128
// 进行数据库的信息的检查,以及将数据库信息写入文件
127
- public function updateDatabase ($ editForm )
129
+ public function updateDatabaseConfig ($ editForm )
128
130
{
129
131
$ host = $ editForm ['host ' ];
130
132
$ database = $ editForm ['database ' ];
@@ -242,42 +244,24 @@ public function getErrorHtml($errorInfo){
242
244
*/
243
245
public function runMigrate ()
244
246
{
247
+ $ bashPath = dirname (Yii::getAlias ('@appfront ' ));
245
248
$ oldApp = \Yii::$ app ;
246
249
Yii::$ app = new \yii \console \Application ([
247
250
'id ' => 'install-console ' ,
248
- 'basePath ' => ' @appfront ' ,
251
+ 'basePath ' => $ bashPath ,
249
252
'components ' => [
250
253
'db ' => $ oldApp ->db ,
251
254
],
252
255
]);
253
- $ runResult = \Yii::$ app ->runAction ('migrate/up ' , ['migrationPath ' => '@fecshop/migrations/mysqldb ' , 'interactive ' => false ]);
254
- \Yii::$ app = $ oldApp ;
255
-
256
- return $ runResult ;
257
- }
258
-
259
- /*
260
- public function runMigrate()
261
- {
262
-
263
- // 通过ob函数截取输出字符
264
256
ob_start ();
265
257
ob_implicit_flush (false );
266
- extract(['oldApp' => \Yii::$app], EXTR_OVERWRITE);
267
- \Yii::$app = new \yii\console\Application([
268
- 'id' => 'install-console',
269
- 'basePath' => '@appfront',
270
- 'components' => [
271
- 'db' => $oldApp->db,
272
- ],
273
- ]);
274
- $result = \Yii::$app->runAction('migrate/up', ['migrationPath' => '@fecshop/migrations/mysqldb', 'interactive' => false]);
258
+ $ runResult = \Yii::$ app ->runAction ('migrate/up ' , ['migrationPath ' => '@fecshop/migrations/mysqldb ' , 'interactive ' => false ]);
259
+ $ post_log = ob_get_clean ();
260
+ Yii::info ($ post_log , 'fecshop_debug ' );
275
261
\Yii::$ app = $ oldApp ;
276
- $this->_migrateLog = ob_get_clean();
277
-
278
- return true;
262
+ // $runResult 返回值,0代表执行完成,1代表执行出错。
263
+ return $ runResult === 0 ? true : false ;
279
264
}
280
- */
281
265
282
266
// 创建文件夹,在图片文件复制的过程中使用。
283
267
public function dirMkdir ($ path = '' , $ mode = 0777 , $ recursive = true )
0 commit comments