1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: vadim |
5
|
|
|
* Date: 22.01.15 |
6
|
|
|
* Time: 13:47 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace sibds\components; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\behaviors\TimestampBehavior; |
13
|
|
|
use sibds\behaviors\UserDataBehavior; |
14
|
|
|
use sibds\behaviors\TrashBehavior; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class ActiveRecord extends \yii\db\ActiveRecord |
19
|
|
|
{ |
20
|
|
|
use BeforeQueryTrait; |
21
|
|
|
|
22
|
|
|
//Status state |
23
|
|
|
const STATUS_UNLOCK = 0; |
24
|
|
|
const STATUS_LOCK = 1; //Blocking records |
25
|
|
|
|
26
|
|
|
public static $BEFORE_QUERY = ['locked' => self::STATUS_UNLOCK]; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
// Dynamical fields for behaviors |
30
|
|
|
/** |
31
|
|
|
* @var string the attribute that will receive timestamp value |
32
|
|
|
* Set this property to false if you do not want to record the creation time. |
33
|
|
|
*/ |
34
|
|
|
public $createdAtAttribute = 'created_at'; |
35
|
|
|
/** |
36
|
|
|
* @var string the attribute that will receive timestamp value. |
37
|
|
|
* Set this property to false if you do not want to record the update time. |
38
|
|
|
*/ |
39
|
|
|
public $updatedAtAttribute = 'updated_at'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string the attribute that will receive current user ID value |
43
|
|
|
* Set this property to false if you do not want to record the creator ID. |
44
|
|
|
*/ |
45
|
|
|
public $createdByAttribute = 'created_by'; |
46
|
|
|
/** |
47
|
|
|
* @var string the attribute that will receive current user ID value |
48
|
|
|
* Set this property to false if you do not want to record the updater ID. |
49
|
|
|
*/ |
50
|
|
|
public $updatedByAttribute = 'updated_by'; |
51
|
|
|
|
52
|
|
|
public $lockedAttribute = 'locked'; |
53
|
|
|
|
54
|
|
|
public $removedAttribute = 'removed'; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function behaviors() |
58
|
|
|
{ |
59
|
|
|
/*Sources: |
60
|
|
|
* https://yii2framework.wordpress.com/2014/11/15/yii-2-behaviors-blameable-and-timestamp/comment-page-1/ |
61
|
|
|
* https://toster.ru/q/82962 |
62
|
|
|
* */ |
63
|
|
|
// If table not have fields, then behavior not use |
64
|
|
|
$behaviors = []; |
65
|
|
|
//Check timestamp |
66
|
|
|
if ($this->hasAttribute($this->createdAtAttribute) && $this->hasAttribute($this->updatedAtAttribute)) { |
67
|
|
|
$behaviors['timestamp'] = [ |
68
|
|
|
'class' => TimestampBehavior::className(), |
69
|
|
|
'attributes' => [ |
70
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute], |
71
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute, |
72
|
|
|
], |
73
|
|
|
|
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
//Check blameable |
78
|
|
|
if ($this->hasAttribute($this->createdByAttribute) && $this->hasAttribute($this->updatedByAttribute)) { |
79
|
|
|
$behaviors['blameable'] = [ |
80
|
|
|
'class' => UserDataBehavior::className(), |
81
|
|
|
'attributes' => [ |
82
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute], |
83
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute, |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
//Check trash |
89
|
|
|
if ($this->hasAttribute($this->removedAttribute)) { |
90
|
|
|
$behaviors['trash'] = [ |
91
|
|
|
'class' => TrashBehavior::className(), |
92
|
|
|
'trashAttribute' => $this->removedAttribute, |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//Check locked |
97
|
|
|
if ($this->hasAttribute($this->lockedAttribute)) { |
98
|
|
|
$behaviors['locked'] = [ |
99
|
|
|
'class' => LockedBehavior::className(), |
100
|
|
|
'lockedAttribute' => $this->lockedAttribute, |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if($this->isNestedSet()){ |
105
|
|
|
$behaviors['tree'] = ArrayHelper::merge([ |
106
|
|
|
'class' => \creocoder\nestedsets\NestedSetsBehavior::className(), |
107
|
|
|
'leftAttribute' => 'lft', |
108
|
|
|
'rightAttribute' => 'rgt', |
109
|
|
|
'depthAttribute' => 'depth', |
110
|
|
|
], ($this->hasAttribute('tree')?['treeAttribute' => 'tree']:[])); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $behaviors; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function isNestedSet(){ |
117
|
|
|
return $this->hasAttribute('lft')&&$this->hasAttribute('rgt')&&$this->hasAttribute('depth'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Duplicate entries in the table. |
122
|
|
|
* @return $this|null |
123
|
|
|
*/ |
124
|
|
|
public function duplicate() { |
125
|
|
|
$this->isNewRecord = true; |
126
|
|
|
|
127
|
|
|
foreach ($this->primaryKey() as $key) { |
128
|
|
|
$this->$key = null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($this->save()) { |
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|