1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\apprelease\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\Url; |
7
|
|
|
use yii\web\UploadedFile; |
8
|
|
|
use yii\behaviors\TimestampBehavior; |
9
|
|
|
use zacksleo\yii2\apprelease\Module; |
10
|
|
|
use zacksleo\yii2\apprelease\behaviors\UploadBehavior; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This is the model class for table "{{%app_release}}". |
14
|
|
|
* |
15
|
|
|
* @property integer $id |
16
|
|
|
* @property integer $version |
17
|
|
|
* @property integer $is_forced |
18
|
|
|
* @property string $url |
19
|
|
|
* @property string $md5 |
20
|
|
|
* @property integer $status |
21
|
|
|
* @property string $description |
22
|
|
|
* @property integer $created_at |
23
|
|
|
* @property integer $updated_at |
24
|
|
|
* @property UploadedFile $file |
25
|
|
|
*/ |
26
|
|
|
class AppRelease extends \yii\db\ActiveRecord |
27
|
|
|
{ |
28
|
|
|
public $file; |
29
|
|
|
const STATUS_PUBLISHED = 1; |
30
|
|
|
const STATUS_UNPUBLISHED = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
9 |
|
public static function tableName() |
36
|
|
|
{ |
37
|
9 |
|
return '{{%app_release}}'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
5 |
|
public function rules() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
5 |
|
[['version', 'description'], 'required'], |
47
|
|
|
[['is_forced', 'status'], 'integer'], |
48
|
|
|
[['is_forced', 'status'], 'default', 'value' => 1], |
49
|
|
|
[['description', 'version'], 'string', 'max' => 255], |
50
|
|
|
['md5', 'string', 'max' => 255, 'on' => ['save']], |
51
|
|
|
[['url'], 'file', |
52
|
|
|
//'extensions' => 'apk', |
53
|
|
|
'skipOnEmpty' => true, |
54
|
|
|
'tooBig' => 'app文件大小不超过120M', |
55
|
|
|
'maxFiles' => 1, |
56
|
|
|
'maxSize' => 120 * 1024 * 1024, |
57
|
|
|
'on' => ['insert', 'update'] |
58
|
|
|
], |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
2 |
|
public function attributeLabels() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
2 |
|
'id' => Module::t('apprelease', 'ID'), |
69
|
2 |
|
'version' => Module::t('apprelease', 'version'), |
70
|
2 |
|
'is_forced' => Module::t('apprelease', 'is forced'), |
71
|
2 |
|
'url' => Module::t('apprelease', 'url'), |
72
|
2 |
|
'md5' => Module::t('apprelease', 'MD5'), |
73
|
2 |
|
'status' => Module::t('apprelease', 'status'), |
74
|
2 |
|
'description' => Module::t('apprelease', 'description'), |
75
|
2 |
|
'created_at' => Module::t('apprelease', 'created at'), |
76
|
2 |
|
'updated_at' => Module::t('apprelease', 'updated at'), |
77
|
2 |
|
'file' => Module::t('apprelease', 'File'), |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
10 |
|
public function behaviors() |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
10 |
|
'timestamp' => [ |
88
|
10 |
|
'class' => TimestampBehavior::className(), |
89
|
|
|
], |
90
|
|
|
[ |
91
|
10 |
|
'class' => UploadBehavior::className(), |
92
|
10 |
|
'attribute' => 'url', |
93
|
|
|
'scenarios' => ['insert', 'update'], |
94
|
10 |
|
'path' => '@frontend/web/uploads/apps', |
95
|
10 |
|
'url' => '@web/uploads/apps', |
96
|
|
|
], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function fields() |
101
|
|
|
{ |
102
|
1 |
|
$fields = parent::fields(); |
103
|
1 |
|
$fields['url'] = function () { |
104
|
1 |
|
$path = str_replace('api/uploads/', '', $this->getUploadUrl('url')); |
|
|
|
|
105
|
1 |
|
if (isset($_ENV['API_HOST'])) { |
106
|
|
|
$url = $_ENV['API_HOST'] . 'files/' . $path; |
107
|
|
|
} else { |
108
|
1 |
|
$url = Url::to(['file/view', 'path' => $path], true); |
109
|
|
|
} |
110
|
1 |
|
return $url; |
111
|
|
|
}; |
112
|
1 |
|
unset($fields['id'], $fields['created_at'], $fields['status'], $fields['updated_at']); |
113
|
1 |
|
return $fields; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: