-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserDataBehavior.php
88 lines (70 loc) · 2.15 KB
/
UserDataBehavior.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
<?php
/**
* Created by PhpStorm.
* User: vadim
* Date: 26.05.15
* Time: 15:12
*/
namespace sibds\behaviors;
use yii\behaviors\BlameableBehavior;
class UserDataBehavior extends BlameableBehavior {
public $userClass = null;
public function init()
{
if (is_null($this->userClass)) {
$this->userClass = \Yii::$app->user->className();
}
}
/**
* @getCreateUser
* @return null|\yii\db\ActiveQuery
*/
public function getCreateUser()
{
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) {
return $owner->hasOne($this->userClass, ['id' => $this->createdByAttribute]);
}
return null;
}
/**
* @getCreateUserName
* @return null|\yii\db\ActiveQuery
*/
public function getCreateUserName()
{
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) {
return $this->createUser ? $this->createUser->username : '- no user -';
}
return null;
}
/**
* @getUpdateUser
* @return null|\yii\db\ActiveQuery
*/
public function getUpdateUser()
{
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) {
return $this->hasOne($this->userClass, ['id' => $this->updatedByAttribute]);
}
return null;
}
/**
* @getUpdateUserName
* @return null|\yii\db\ActiveQuery
*/
public function getUpdateUserName()
{
/* @var $owner BaseActiveRecord */
$owner = $this->owner;
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) {
return $this->createUser ? $this->updateUser->username : '- no user -';
}
return null;
}
}