-
Notifications
You must be signed in to change notification settings - Fork 3
/
UserPhone.php
103 lines (91 loc) · 2.18 KB
/
UserPhone.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
<?php
namespace lav45\behaviors\tests\models;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use lav45\behaviors\PushBehavior;
/**
* Class UserPhone
* @package lav45\behaviors\tests\models
*
* @property int $id
* @property int $user_id
* @property int $type
* @property int $phone
*
* @property ApiUser $apiUser
* @property User $user
*/
class UserPhone extends ActiveRecord
{
/**
* @return array
*/
public function transactions()
{
return [
self::SCENARIO_DEFAULT => self::OP_ALL,
];
}
/**
* @return array
*/
public function behaviors()
{
return [
[
'class' => PushBehavior::class,
'relation' => 'apiUser',
'deleteRelation' => [$this, 'deleteRelation'],
'createRelation' => false,
'updateRelationAfterInsert' => true,
'attributes' => [
'type' => [
'field' => 'phones',
'value' => 'apiPhones',
],
]
]
];
}
/**
* @param ApiUser $model
*/
public function deleteRelation(ApiUser $model)
{
$model->phones = $this->getApiPhones(true);
$model->save(false);
}
/**
* @param bool $excludingSelf
* @return string
*/
public function getApiPhones($excludingSelf = false)
{
$query = self::find()
->select(['id', 'phone', 'type'])
->where(['user_id' => $this->user_id])
->asArray();
if ($excludingSelf) {
$query->andWhere(['not', ['id' => $this->id]]);
}
$phones = $query->all();
if ($phones) {
$phones = ArrayHelper::map($phones, 'id', 'phone', 'type');
}
return json_encode($phones);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getApiUser()
{
return $this->hasOne(ApiUser::class, ['id' => 'user_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function User()
{
return $this->hasOne(User::class, ['id' => 'user_id']);
}
}