-
Notifications
You must be signed in to change notification settings - Fork 10
/
WebAction.php
89 lines (79 loc) · 2.43 KB
/
WebAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace e282486518\migration;
use Yii;
use yii\base\Action;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
class WebAction extends Action
{
/**
* @var string $returnFormat 返回格式,json/txt
*/
public $returnFormat = 'json';
/**
* @var string migration class files 保存路径
*/
public $migrationPath = '@console/migrations';
/**
* 处理action
*/
public function run()
{
$name = Yii::$app->request->get('tables');
$name = trim($name,',');
if(strpos($name, ',')){
/* 备份部分数据表 */
$tables = explode(',', $name);
} else {
/* 备份一个数据表 */
$tables = [$name];
}
/* 所有数据表 */
$alltables = Yii::$app->db->createCommand('SHOW TABLE STATUS')->queryAll();
$alltables = array_map('array_change_key_case', $alltables);
$alltables = ArrayHelper::getColumn($alltables, 'name');
/* 检查表是否存在 */
foreach ($tables as $table) {
if (!in_array($table,$alltables)) {
if ($this->returnFormat == 'json') {
$this->ajaxReturn(1,$table.' table no find ...',$table);
} else {
echo $table.' table no find ...';
}
}
}
/* 创建migration */
foreach ($tables as $table) {
$migrate = Yii::createObject([
'class' => 'e282486518\migration\components\MigrateCreate',
'migrationPath' => $this->migrationPath
]);
$migrate->create($table);
unset($migrate);
}
if ($this->returnFormat == 'json') {
$this->ajaxReturn(0,'backup success.');
} else {
echo 'backup success..';
}
}
/**
* ---------------------------------------
* ajax标准返回格式
* @param $code integer 错误码
* @param $msg string 提示信息
* @param $obj mixed 返回数据
* @return void
* ---------------------------------------
*/
public function ajaxReturn($code = 0, $msg = 'success', $obj = ''){
/* api标准返回格式 */
$json = array(
'code' => $code,
'msg' => $msg,
'obj' => $obj,
);
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($json));
}
}