Completed
Push — master ( cf382f...18c5d0 )
by zacksleo
03:24
created

ShippingAddress::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 16
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
crap 1
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 1
    public function rules()
39
    {
40
        return [
41 1
            [['user_id', 'phone', 'name', 'street', 'district', 'city', 'province'], 'required'],
42
            [['user_id'], 'integer'],
43 1
            ['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
    public function getAddress($glue = '')
74
    {
75
        return implode($glue, [$this->country, $this->province, $this->city, $this->district]);
76
    }
77
78
    /**
79
     * @param string $glue
80
     * @return string
81
     */
82
    public function getFullAddress($glue = '')
83
    {
84
        return implode($glue, [$this->country, $this->province, $this->city, $this->district, $this->street]);
85
    }
86
87
    public function afterSave($insert, $changedAttributes)
88
    {
89
        parent::afterSave($insert, $changedAttributes);
90
        if ($this->status == self::STATUS_ACTIVE) {
91
            self::updateAll(['status' => 0], [
92
                'and',
93
                ['user_id' => $this->user_id],
94
                [' <> ', 'id', $this->id]
95
            ]);
96
        }
97
    }
98
}
99