-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUser.php
153 lines (135 loc) · 3.57 KB
/
User.php
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
<?php
namespace lav45\behaviors\tests\models;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use lav45\behaviors\PushBehavior;
/**
* Class User
* @package lav45\behaviors\tests\models
*
* @property int $id
* @property string $login
* @property string $first_name
* @property string $last_name
* @property int $created_at
* @property int $updated_at
* @property int $last_login
* @property int $company_id
*
* @property ApiUser $apiUser
* @property Company $company
* @property UserProfile $profile
* @property UserEmail $email
* @property UserPhone[] $phones
*/
class User extends ActiveRecord
{
/**
* @return array
*/
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
/**
* @return array
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
],
[
'class' => PushBehavior::class,
'relation' => 'apiUser',
'attributes' => [
'id',
'login' => 'user_login',
'updated_at' => [
'field' => 'updatedAt',
'value' => 'data.updated',
],
'created_at' => [
'field' => 'createdAt',
'value' => ['data', 'created'],
],
'last_login' => [
'field' => 'lastLogin',
'value' => function (self $owner) {
return $owner->last_login;
}
],
'fio' => [
'watch' => ['first_name', 'last_name'],
],
'company_id' => 'company_id',
[
'watch' => 'company_id', // if changed attribute `company_id`
'value' => 'company.name', // then get value from the $this->company->name
'field' => 'company_name', // and set this value in to the relation attribute `company_name`
]
]
]
];
}
/**
* @return string
*/
public function getFio()
{
if (empty($this->last_name)) {
return $this->first_name;
}
return $this->first_name . ' ' . $this->last_name;
}
/**
* @return array
*/
public function getData()
{
return [
'login' => $this->login,
'updated' => $this->updated_at,
'created' => $this->created_at,
'last_login' => $this->last_login,
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getApiUser()
{
return $this->hasOne(ApiUser::class, ['id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCompany()
{
return $this->hasOne(Company::class, ['id' => 'company_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProfile()
{
return $this->hasOne(UserProfile::class, ['user_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEmail()
{
return $this->hasOne(UserEmail::class, ['user_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPhones()
{
return $this->hasMany(UserPhone::class, ['user_id' => 'id']);
}
}