1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\shop\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This is the model class for table "mops_shipping_address". |
9
|
|
|
* |
10
|
|
|
* @property integer $id |
11
|
|
|
* @property integer $user_id |
12
|
|
|
* @property string $phone |
13
|
|
|
* @property string $name |
14
|
|
|
* @property string $street |
15
|
|
|
* @property string $postcode |
16
|
|
|
* @property string $district |
17
|
|
|
* @property string $city |
18
|
|
|
* @property string $province |
19
|
|
|
* @property string $country |
20
|
|
|
* @property boolean $status |
21
|
|
|
*/ |
22
|
|
|
class ShippingAddress extends \yii\db\ActiveRecord |
23
|
|
|
{ |
24
|
|
|
const STATUS_INACTIVE = 0; |
25
|
|
|
const STATUS_ACTIVE = 1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
1 |
|
public static function tableName() |
31
|
|
|
{ |
32
|
1 |
|
return '{{%shipping_address}}'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
2 |
|
public function rules() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
2 |
|
[['user_id', 'phone', 'name', 'street', 'district', 'city', 'province'], 'required'], |
42
|
|
|
[['user_id'], 'integer'], |
43
|
2 |
|
['status', 'in', 'range' => [self::STATUS_INACTIVE, self::STATUS_ACTIVE]], |
44
|
|
|
[['phone', 'name', 'street', 'district', 'city', 'province', 'country'], 'string', 'max' => 255], |
45
|
|
|
[['postcode'], 'string', 'max' => 10], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
1 |
|
public function attributeLabels() |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
1 |
|
'id' => 'ID', |
56
|
|
|
'user_id' => '用户', |
57
|
|
|
'phone' => '手机号码', |
58
|
|
|
'name' => '姓名', |
59
|
|
|
'street' => '街道', |
60
|
|
|
'postcode' => '邮编', |
61
|
|
|
'district' => '区', |
62
|
|
|
'city' => '市', |
63
|
|
|
'province' => '省', |
64
|
|
|
'country' => '国家', |
65
|
|
|
'status' => '状态', |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $glue |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
1 |
|
public function getAddress($glue = '') |
74
|
|
|
{ |
75
|
1 |
|
return implode($glue, [$this->country, $this->province, $this->city, $this->district]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $glue |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function getFullAddress($glue = '') |
83
|
|
|
{ |
84
|
1 |
|
return implode($glue, [$this->country, $this->province, $this->city, $this->district, $this->street]); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function afterSave($insert, $changedAttributes) |
88
|
|
|
{ |
89
|
1 |
|
parent::afterSave($insert, $changedAttributes); |
90
|
1 |
|
if ($this->status == self::STATUS_ACTIVE) { |
91
|
1 |
|
self::updateAll(['status' => 0], [ |
92
|
1 |
|
'and', |
93
|
1 |
|
['user_id' => $this->user_id], |
94
|
1 |
|
[' <> ', 'id', $this->id] |
95
|
|
|
]); |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
} |
99
|
|
|
|