Tablemake.php
28.3 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Db;
use think\Config;
//use think\Config;
/**
* 自建表管理
*
* @icon fa fa-circle-o
*/
class Tablemake extends Backend {
/**
* Test模型对象
* @var \app\admin\model\Test
*/
protected $ModelOnline = null;
protected $ModelFields = null;
public function _initialize() {
parent::_initialize();
$this->ModelOnline = model('TableMakeTables');
$this->ModelFields = model('TableMakeFields');
}
/*
* 模块列表
*/
public function index() {
if ($this->request->isAjax()) {
//weigh desc,
$list = $this->ModelOnline->order('id desc')->select();
$total = count($list);
$prefix = Config::get('database.prefix');
$result = array("total" => $total, "rows" => $list, "prefix" => $prefix);
return json($result);
}
return $this->view->fetch();
}
/*
* 添加模块
*/
public function add() {
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->ModelOnline));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
$this->ModelOnline->validate($validate);
}
$params['createtime'] = time();
$params['updatetime'] = time();
$result = $this->ModelOnline->allowField(true)->save($params);
$prefix = Config::get('database.prefix');
if ($result !== false) {
//在此执行创建表的操作
$sql = "CREATE TABLE IF NOT EXISTS `{$prefix}" . $params['table'] . "` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='" . $params['name'] . "';";
$res = \think\Db::execute($sql);
//var_export($res);
$this->success();
} else {
$this->error($this->ModelOnline->getError());
}
} catch (\think\exception\PDOException $e) {
$this->error($e->getMessage());
} catch (\think\Exception $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$prefix = Config::get('database.prefix');
$this->view->assign("prefix", $prefix);
return $this->view->fetch();
}
/*
* 编辑模块
*/
public function edit($ids = NULL) {
$row = $this->ModelOnline->get($ids);
if (!$row)
$this->error(__('No Results were found'));
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = basename(str_replace('\\', '/', get_class($this->ModelOnline)));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
$row->validate($validate);
}
$params['updatetime'] = time();
$result = $row->allowField(true)->save($params);
if ($result !== false) {
$prefix = Config::get('database.prefix');
$sql = "ALTER TABLE `{$prefix}" . $row['table'] . "` COMMENT='" . $row['name'] . "';";
$res = \think\Db::execute($sql);
$this->success();
} else {
$this->error($row->getError());
}
} catch (\think\exception\PDOException $e) {
$this->error($e->getMessage());
} catch (\think\Exception $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$prefix = Config::get('database.prefix');
$this->view->assign("row", $row);
$this->view->assign("prefix", $prefix);
return $this->view->fetch();
}
/*
* 删除模块
*/
public function del($ids = NULL) {
if ($ids) {
$pk = $this->ModelOnline->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$count = $this->ModelOnline->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->ModelOnline->where($pk, 'in', $ids)->select();
$prefix = Config::get('database.prefix');
$count = 0;
foreach ($list as $k => $v) {
$sql = "DROP TABLE IF EXISTS `{$prefix}" . $v->table . "`;";
try {
$res = \think\Db::execute($sql);
$this->ModelFields->where("mid", '=', $v->id)->delete();
$count += $v->delete();
if ($count) {
$this->success(__('删除成功!'), null, __('删除成功!'));
} else {
$this->error(__('No rows were deleted'));
}
} catch (Exception $ex) {
$this->error(__('No rows were deleted'));
}
}
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
/*
* 验证重名
*/
public function check($table = null, $name = null) {
if ($table == null && $name == null) {
if ($this->request->isAjax()) {
$table = $this->request->request('table');
$name = $this->request->request('name');
}
}
if ($table && $name) {
$sql = "describe `{$table}` `{$name}`";
$res = \think\Db::query($sql);
if ($res) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/*
* 字段列表
*/
public function fields($ids = NULL) {
if ($ids == NULL) {
$ids = intval($this->request->request('ids'));
}
$model = $this->ModelOnline->get($ids);
if (!$model) {
$this->error(__('No Results were found'));
}
if ($this->request->isAjax()) {
$list = $this->ModelFields->where("mid", '=', $ids)->order('id desc')->select();
$total = count($list);
$prefix = Config::get('database.prefix');
$result = array("total" => $total, "rows" => $list, "prefix" => $prefix);
return json($result);
}
$this->view->assign("ids", $ids);
return $this->view->fetch();
}
/*
* 添加字段
*/
public function field_add($mid = NULL) {
$mod_table = $this->ModelOnline->get($mid);
if (!$mod_table)
$this->error(__('No Results were found'));
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->ModelFields));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
$this->ModelFields->validate($validate);
}
$prefix = Config::get('database.prefix');
$field = array();
$fieldData = array();
switch ($params['category']) {
case "1":
$allow = array(
"text" => array("suffix" => "text", "type" => "varchar", "length" => 200),
"number" => array("suffix" => "number", "type" => "int", "length" => 10),
"time" => array("suffix" => "time", "type" => "int", "length" => 11),
"image" => array("suffix" => "image", "type" => "varchar", "length" => 255),
"images" => array("suffix" => "images", "type" => "varchar", "length" => 2000),
"file" => array("suffix" => "file", "type" => "varchar", "length" => 255),
"files" => array("suffix" => "files", "type" => "varchar", "length" => 2000),
"avatar" => array("suffix" => "avatar", "type" => "varchar", "length" => 255),
"avatars" => array("suffix" => "avatars", "type" => "varchar", "length" => 2000),
"content" => array("suffix" => "content", "type" => "text", "length" => 0),
"_id" => array("suffix" => "_id", "type" => "int", "length" => 10),
"_ids" => array("suffix" => "_ids", "type" => "varchar", "length" => 255),
"list-enum" => array("suffix" => "list", "type" => "enum", "length" => 0),
"list-set" => array("suffix" => "list", "type" => "set", "length" => 0),
"data-enum" => array("suffix" => "data", "type" => "enum", "length" => 0),
"data-set" => array("suffix" => "data", "type" => "set", "length" => 0),
"switch" => array("suffix" => "switch", "type" => "tinyint", "length" => 1),
);
if (isset($allow[$params['suffix']]) && is_array($allow[$params['suffix']])) {
$fieldData['special'] = "";
$fieldData['suffix'] = $params['suffix'];
//$field['name'] = $params['name'] . $allow[$params['suffix']]['suffix']; //字段名
$field['name'] = $params['name']; //字段名
$field['field']=$params['name'] . $allow[$params['suffix']]['suffix']; //字段名
$field['length'] = $field['length2'] = isset($params['length']) && intval($params['length']) ? intval($params['length']) : $allow[$params['suffix']]['length']; //字段长度
$field['type'] = $allow[$params['suffix']]['type']; //字段类型
$field['default'] = isset($params['default']) ? $params['default'] : ""; //默认值
if ($allow[$params['suffix']]['type'] == "enum" || $allow[$params['suffix']]['type'] == "set") {
$comment = \GuzzleHttp\json_decode($params['comment'], true);
$field['comment'] = $params['title'] . ":"; //备注
$field['length2'] = "";
$str = "";
$default_optional = array();
foreach ($comment as $k => $v) {
$default_optional[] = $k;
$field['comment'] .= $str . $k . "=" . $v;
$field['length2'] .= $str . "'" . $k . "'";
$str = ",";
}
if (!in_array($field['default'], $default_optional)) {
$field['default'] = $default_optional[0];
}
} else {
$params['comment'] = "";
$field['comment'] = $params['title']; //备注
}
} else {
$this->error(__('特殊字段类型不正确!'));
}
break;
case "2":
$allow = array(
"varchar" => array("type" => "varchar", "length" => 255),
"int" => array("type" => "int", "length" => 10),
"enum" => array("type" => "enum", "length" => 0),
"set" => array("type" => "set", "length" => 0),
"float" => array("type" => "float", "length" => "10,2"),
"text" => array("type" => "text", "length" => 0),
"datetime" => array("type" => "datetime", "length" => 10),
"date" => array("type" => "date", "length" => 10),
"year" => array("type" => "year", "length" => 4),
"timestamp" => array("type" => "timestamp", "length" => 10),
);
if (isset($allow[$params['type']]) && is_array($allow[$params['type']])) {
$fieldData['special'] = "";
$fieldData['suffix'] = "";
$params['suffix'] = "";
$field['name'] = $params['name']; //字段名
$field['field']= $params['name']; //字段名
if ($params['type'] == "float") {
$field['length'] = $field['length2'] = isset($params['length']) && $params['length'] ? $params['length'] : $allow[$params['type']]['length']; //字段长度
} else {
$field['length'] = $field['length2'] = isset($params['length']) && intval($params['length']) ? intval($params['length']) : $allow[$params['type']]['length']; //字段长度
}
$field['type'] = $allow[$params['type']]['type']; //字段类型
$field['default'] = isset($params['default']) ? $params['default'] : ""; //默认值
if ($allow[$params['type']]['type'] == "enum" || $allow[$params['type']]['type'] == "set") {
$comment = \GuzzleHttp\json_decode($params['comment'], true);
$field['comment'] = $params['title'] . ":"; //备注
$field['length2'] = "";
$str = "";
$default_optional = array();
foreach ($comment as $k => $v) {
$default_optional[] = $k;
$field['comment'] .= $str . $k . "=" . $v;
$field['length2'] .= $str . "'" . $k . "'";
$str = ",";
}
if (!in_array($field['default'], $default_optional)) {
$field['default'] = $default_optional[0];
}
} else {
$params['comment'] = "";
$field['comment'] = $params['title']; //备注
}
} else {
$this->error(__('特殊字段类型不正确!'));
}
break;
case "3":
$allow = array(
"category_id" => array("title" => "分类ID(单选)", "type" => "int", "length" => 10),
"category_ids" => array("title" => "分类ID(多选)", "type" => "varchar", "length" => 200),
"weigh" => array("title" => "权重", "type" => "int", "length" => 10),
"createtime" => array("title" => "创建时间", "type" => "int", "length" => 10),
"updatetime" => array("title" => "更新时间", "type" => "int", "length" => 10)
);
if (isset($allow[$params['special']]) && is_array($allow[$params['special']])) {
$fieldData['special'] = $params['special'];
$fieldData['suffix'] = "";
//$params['title'] = $allow[$params['special']]['title'];
$params['comment'] = $params['suffix'] = "";
$field['name'] = $params['special']; //字段名
$field['field']= $params['special']; //字段名
$field['length'] = $field['length2'] = $allow[$params['special']]['length']; //字段长度
$field['comment'] = $params['title']; //备注
$field['type'] = $allow[$params['special']]['type']; //字段类型
$field['default'] = $field['type'] == "varchar" ? "" : "0"; //默认值
} else {
$this->error(__('特殊字段类型不正确!'));
}
break;
default :
$this->error(__('No Results were found'));
break;
}
if ($this->check($prefix . $mod_table['table'], $field['name'])) {
$this->error(__('字段已经存在!'));
}
$fieldData['mid'] = $params['mid'];
$fieldData['category'] = $params['category'];
$fieldData['title'] = $params['title'];
$fieldData['name'] = $field['name'];
$fieldData['field'] = $field['field'];
$fieldData['type'] = $field['type'];
$fieldData['length'] = $field['length'];
$fieldData['default'] = $field['default'];
$fieldData['comment'] = $field['comment'];
$fieldData['createtime'] = time();
$fieldData['updatetime'] = time();
if ($fieldData['type'] == "text") {
$fieldData['default'] = "";
}
$result = $this->ModelFields->allowField(true)->save($fieldData);
if ($field['type'] == "int") {
$field['default'] = intval($field['default']);
} elseif ($field['type'] == "tinyint") {
$field['default'] = in_array($field['default'], [0, 1]) ? $field['default'] : 0;
} elseif ($field['type'] == "float") {
$field['default'] = is_float($field['default']) ? $field['default'] : 0;
}
if ($result !== false) {
//在此执行添加字段的操作
if (in_array($field['type'], ["text", "datetime", "date", "year", "timestamp"])) {
$sql = "ALTER TABLE `{$prefix}{$mod_table['table']}` ADD COLUMN `{$field['field']}` {$field['type']} NOT NULL COMMENT '{$field['comment']}';";
} else {
$sql = "ALTER TABLE `{$prefix}{$mod_table['table']}` ADD COLUMN `{$field['field']}` {$field['type']}({$field['length2']}) NOT NULL DEFAULT '{$field['default']}' COMMENT '{$field['comment']}';";
}
try {
$res = \think\Db::execute($sql);
$this->success();
} catch (Exception $ex) {
//$result->delete();
$this->error(__('参数错误,请检查字段名,字段长度或者默认值等输入参数是否合法', ''));
}
} else {
$this->error($this->ModelFields->getError());
}
} catch (\think\exception\PDOException $e) {
$this->error($e->getMessage());
} catch (\think\Exception $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$prefix = Config::get('database.prefix');
$this->view->assign("prefix", $prefix);
$this->view->assign("mid", $mid);
return $this->view->fetch();
}
/*
* 修改字段
*/
public function field_edit($ids = NULL) {
//$oldField_info = $this->ModelFields->get($ids);
$field_info = $this->ModelFields->get($ids);
$oldField_info=$field_info->toArray();
//var_dump($field_info);
if (!$field_info)
$this->error(__('No Results were found'));
$mod_table = $this->ModelOnline->get($field_info['mid']);
if (!$mod_table)
$this->error(__('No Results were found'));
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->ModelFields));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
$this->ModelFields->validate($validate);
}
$prefix = Config::get('database.prefix');
$field = array();
$fieldData = array();
switch ($field_info['category']) {
case "1":
$allow = array(
"text" => array("suffix" => "text", "type" => "varchar", "length" => 200),
"number" => array("suffix" => "number", "type" => "int", "length" => 10),
"time" => array("suffix" => "time", "type" => "int", "length" => 11),
"image" => array("suffix" => "image", "type" => "varchar", "length" => 255),
"images" => array("suffix" => "images", "type" => "varchar", "length" => 2000),
"file" => array("suffix" => "file", "type" => "varchar", "length" => 255),
"files" => array("suffix" => "files", "type" => "varchar", "length" => 2000),
"avatar" => array("suffix" => "avatar", "type" => "varchar", "length" => 255),
"avatars" => array("suffix" => "avatars", "type" => "varchar", "length" => 2000),
"content" => array("suffix" => "content", "type" => "text", "length" => 0),
"_id" => array("suffix" => "_id", "type" => "int", "length" => 10),
"_ids" => array("suffix" => "_ids", "type" => "varchar", "length" => 255),
"list-enum" => array("suffix" => "list", "type" => "enum", "length" => 0),
"list-set" => array("suffix" => "list", "type" => "set", "length" => 0),
"data-enum" => array("suffix" => "data", "type" => "enum", "length" => 0),
"data-set" => array("suffix" => "data", "type" => "set", "length" => 0),
"switch" => array("suffix" => "switch", "type" => "tinyint", "length" => 1),
);
if (isset($allow[$params['suffix']]) && is_array($allow[$params['suffix']])) {
$fieldData['special'] = "";
$fieldData['suffix'] = $params['suffix'];
$field['name'] = $params['name']; //字段名
$field['field']=$params['name'] . $allow[$params['suffix']]['suffix']; //字段名
$field['length'] = $field['length2'] = isset($params['length']) && intval($params['length']) ? intval($params['length']) : $allow[$params['suffix']]['length']; //字段长度
$field['type'] = $allow[$params['suffix']]['type']; //字段类型
$field['default'] = isset($params['default']) ? $params['default'] : ""; //默认值
if ($allow[$params['suffix']]['type'] == "enum" || $allow[$params['suffix']]['type'] == "set") {
$comment = \GuzzleHttp\json_decode($params['comment'], true);
$field['comment'] = $params['title'] . ":"; //备注
$field['length2'] = "";
$str = "";
$default_optional = array();
foreach ($comment as $k => $v) {
$default_optional[] = $k;
$field['comment'] .= $str . $k . "=" . $v;
$field['length2'] .= $str . "'" . $k . "'";
$str = ",";
}
if (!in_array($field['default'], $default_optional)) {
$field['default'] = $default_optional[0];
}
} else {
$params['comment'] = "";
$field['comment'] = $params['title']; //备注
}
} else {
$this->error(__('特殊字段类型不正确!'));
}
break;
case "2":
$allow = array(
"varchar" => array("type" => "varchar", "length" => 255),
"int" => array("type" => "int", "length" => 10),
"enum" => array("type" => "enum", "length" => 0),
"set" => array("type" => "set", "length" => 0),
"float" => array("type" => "float", "length" => "10,2"),
"text" => array("type" => "text", "length" => 0),
"datetime" => array("type" => "datetime", "length" => 10),
"date" => array("type" => "date", "length" => 10),
"year" => array("type" => "year", "length" => 4),
"timestamp" => array("type" => "timestamp", "length" => 10),
);
if (isset($allow[$params['type']]) && is_array($allow[$params['type']])) {
$fieldData['special'] = "";
$fieldData['suffix'] = "";
$params['suffix'] = "";
$field['name'] = $params['name']; //字段名
$field['field']= $params['name']; //字段名
if ($params['type'] == "float") {
$field['length'] = $field['length2'] = isset($params['length']) && $params['length'] ? $params['length'] : $allow[$params['type']]['length']; //字段长度
} else {
$field['length'] = $field['length2'] = isset($params['length']) && intval($params['length']) ? intval($params['length']) : $allow[$params['type']]['length']; //字段长度
}
$field['type'] = $allow[$params['type']]['type']; //字段类型
$field['default'] = isset($params['default']) ? $params['default'] : ""; //默认值
if ($allow[$params['type']]['type'] == "enum" || $allow[$params['type']]['type'] == "set") {
$comment = \GuzzleHttp\json_decode($params['comment'], true);
$field['comment'] = $params['title'] . ":"; //备注
$field['length2'] = "";
$str = "";
$default_optional = array();
foreach ($comment as $k => $v) {
$default_optional[] = $k;
$field['comment'] .= $str . $k . "=" . $v;
$field['length2'] .= $str . "'" . $k . "'";
$str = ",";
}
if (!in_array($field['default'], $default_optional)) {
$field['default'] = $default_optional[0];
}
} else {
$params['comment'] = "";
$field['comment'] = $params['title']; //备注
}
} else {
$this->error(__('特殊字段类型不正确!'));
}
break;
case "3":
$allow = array(
"category_id" => array("title" => "分类ID(单选)", "type" => "int", "length" => 10),
"category_ids" => array("title" => "分类ID(多选)", "type" => "varchar", "length" => 200),
"weigh" => array("title" => "权重", "type" => "int", "length" => 10),
"createtime" => array("title" => "创建时间", "type" => "int", "length" => 10),
"updatetime" => array("title" => "更新时间", "type" => "int", "length" => 10)
);
if (isset($allow[$params['special']]) && is_array($allow[$params['special']])) {
$fieldData['special'] = $params['special'];
$fieldData['suffix'] = "";
//$params['title'] = $allow[$params['special']]['title'];
$params['comment'] = $params['suffix'] = "";
$field['name'] = $params['special']; //字段名
$field['field']= $params['special']; //字段名
$field['length'] = $field['length2'] = $allow[$params['special']]['length']; //字段长度
$field['comment'] = $params['title']; //备注
$field['type'] = $allow[$params['special']]['type']; //字段类型
$field['default'] = $field['type'] == "varchar" ? "" : "0"; //默认值
} else {
$this->error(__('特殊字段类型不正确!'));
}
break;
default :
$this->error(__('No Results were found'));
break;
}
/*
if ($this->check($prefix . $mod_table['table'], $field['name'])) {
$this->error(__('字段已经存在!'));
} */
$fieldData['mid'] = $params['mid'];
//$fieldData['category'] = $params['category'];
$fieldData['title'] = $params['title'];
$fieldData['name'] = $field['name'];
$fieldData['field'] = $field['field'];
$fieldData['type'] = $field['type'];
$fieldData['length'] = $field['length'];
$fieldData['default'] = $field['default'];
$fieldData['comment'] = $field['comment'];
//$fieldData['createtime'] = time();
$fieldData['updatetime'] = time();
if ($fieldData['type'] == "text") {
$fieldData['default'] = "";
}
$result =$field_info->save($fieldData);
if ($field['type'] == "int") {
$field['default'] = intval($field['default']);
} elseif ($field['type'] == "tinyint") {
$field['default'] = in_array($field['default'], [0, 1]) ? $field['default'] : 0;
} elseif ($field['type'] == "float") {
$field['default'] = is_float($field['default']) ? $field['default'] : 0;
}
if ($result !== false) {
//在此执行添加字段的操作
if (in_array($field['type'], ["text", "datetime", "date", "year", "timestamp"])) {
//$sql = "ALTER TABLE `{$prefix}{$mod_table['table']}` ADD COLUMN `{$field['field']}` {$field['type']} NOT NULL COMMENT '{$field['comment']}';";
$sql = "ALTER TABLE `{$prefix}{$mod_table['table']}` CHANGE COLUMN `{$oldField_info['field']}` `{$field['field']}` {$field['type']} NOT NULL COMMENT '{$field['comment']}' ;";
} else {
//$sql = "ALTER TABLE `{$prefix}{$mod_table['table']}` ADD COLUMN `{$field['field']}` {$field['type']}({$field['length2']}) NOT NULL DEFAULT '{$field['default']}' COMMENT '{$field['comment']}';";
$sql = "ALTER TABLE `{$prefix}{$mod_table['table']}` CHANGE COLUMN `{$oldField_info['field']}` `{$field['field']}` {$field['type']}({$field['length2']}) NOT NULL DEFAULT '{$field['default']}' COMMENT '{$field['comment']}' ;";
}
/*
ALTER TABLE `fa_test_01` CHANGE COLUMN `dwadwad` `ddd` varchar(255) NOT NULL DEFAULT '新默认值' COMMENT '新的注释' ;
* */
try {
$res = \think\Db::execute($sql);
$this->success();
} catch (Exception $ex) {
$field_info->save($oldField_info);
$this->error(__('参数错误,请检查字段名,字段长度或者默认值等输入参数是否合法', ''));
}
} else {
$this->error($this->ModelFields->getError());
}
} catch (\think\exception\PDOException $e) {
$field_info->save($oldField_info);
$this->error($e->getMessage());
} catch (\think\Exception $e) {
$field_info->save($oldField_info);
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$comment = "";
if ($field_info['type'] == "enum" || $field_info['type'] == "set") {
//echo $field_info['comment'];
$commentStr=substr($field_info['comment'],strpos($field_info['comment'],":")+1);
$commentArr=[];
foreach (explode(",", $commentStr) as $k=>$v){
list($key,$val)= explode("=", $v);
$commentArr[$key]=$val;
}
$comment = \json_encode($commentArr);
}
$prefix = Config::get('database.prefix');
$this->view->assign("field_info", $field_info);
$this->view->assign("row", $field_info);
$this->view->assign("prefix", $prefix);
$this->view->assign("comment", $comment);
$this->view->assign("mid", $field_info['mid']);
return $this->view->fetch();
}
public function field_del($ids = NULL) {
if ($ids) {
$pk = $this->ModelFields->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$count = $this->ModelFields->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->ModelFields->where($pk, 'in', $ids)->select();
$prefix = Config::get('database.prefix');
$count = 0;
foreach ($list as $k => $v) {
$mod_table = $this->ModelOnline->get($v['mid']);
$sql = "ALTER TABLE `{$prefix}{$mod_table['table']}` DROP `{$v['field']}` ";
try {
$res = \think\Db::execute($sql);
$count += $v->delete();
if ($count) {
$this->success();
} else {
$this->error(__('No rows were deleted'));
}
} catch (Exception $ex) {
$this->error(__('No rows were deleted'));
}
}
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
}