forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateField.php
More file actions
89 lines (74 loc) · 2.29 KB
/
Copy pathUpdateField.php
File metadata and controls
89 lines (74 loc) · 2.29 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
<?php
namespace app\models\field;
use Yii;
use app\models\Field;
use app\models\projectLog\CreateLog;
class UpdateField extends Field
{
/**
* 验证规则
*/
public function rules()
{
return [
['id', 'validateAuth'],
[['header_fields', 'request_fields', 'response_fields'], 'string'],
];
}
/**
* 检测是否有操作权限
* @param $attribute
*/
public function validateAuth($attribute)
{
if(!$this->api->project->hasAuth(['api' => 'update'])){
$this->addError($attribute, '抱歉,您没有操作权限');
return false;
}
}
/**
* 保存接口
* @return bool
*/
public function store()
{
if(!$this->validate()){
return false;
}
// 开启事务
$transaction = Yii::$app->db->beginTransaction();
$field = &$this;
$field->header_fields = $this->header_fields;
$field->request_fields = $this->request_fields;
$field->response_fields = $this->response_fields;
if(array_sum([strlen($this->header_fields), strlen($this->request_fields), strlen($this->response_fields)]) == 0){
$this->addError($field->getErrorLabel(), '至少填写一个字段');
$transaction->rollBack();
return false;
}
// 如果有更改,保存操作日志
if(array_filter($field->dirtyAttributes)) {
$log = new CreateLog();
$log->project_id = $field->api->project->id;
$log->object_name = 'api';
$log->object_id = $field->api->id;
$log->type = 'update';
$log->content = $field->getUpdateContent();
if(!$log->store()){
$this->addError($log->getErrorLabel(), $log->getErrorMessage());
$transaction->rollBack();
return false;
}
}
// 保存字段更新
$field->updater_id = Yii::$app->user->identity->id;
if(!$field->save()){
$this->addError($field->getErrorLabel(), $field->getErrorMessage());
$transaction->rollBack();
return false;
}
// 事务提交
$transaction->commit();
return true;
}
}