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
|
|
|
|
16
|
|
|
|
17
|
|
|
class ActiveRecord extends \yii\db\ActiveRecord |
18
|
|
|
{ |
19
|
|
|
use BeforeQueryTrait; |
20
|
|
|
|
21
|
|
|
//Status state |
22
|
|
|
const STATUS_UNLOCK = 0; |
23
|
|
|
const STATUS_LOCK = 1; //Blocking records |
24
|
|
|
|
25
|
|
|
public static $BEFORE_QUERY = ['removed' => 0, 'locked' => self::STATUS_UNLOCK]; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
// Dynamical fields for behaviors |
29
|
|
|
/** |
30
|
|
|
* @var string the attribute that will receive timestamp value |
31
|
|
|
* Set this property to false if you do not want to record the creation time. |
32
|
|
|
*/ |
33
|
|
|
public $createdAtAttribute = 'created_at'; |
34
|
|
|
/** |
35
|
|
|
* @var string the attribute that will receive timestamp value. |
36
|
|
|
* Set this property to false if you do not want to record the update time. |
37
|
|
|
*/ |
38
|
|
|
public $updatedAtAttribute = 'updated_at'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string the attribute that will receive current user ID value |
42
|
|
|
* Set this property to false if you do not want to record the creator ID. |
43
|
|
|
*/ |
44
|
|
|
public $createdByAttribute = 'created_by'; |
45
|
|
|
/** |
46
|
|
|
* @var string the attribute that will receive current user ID value |
47
|
|
|
* Set this property to false if you do not want to record the updater ID. |
48
|
|
|
*/ |
49
|
|
|
public $updatedByAttribute = 'updated_by'; |
50
|
|
|
|
51
|
|
|
public $lockedAttribute = 'locked'; |
52
|
|
|
|
53
|
|
|
public $removedAttribute = 'removed'; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public function behaviors() |
57
|
|
|
{ |
58
|
|
|
/*Sources: |
|
|
|
|
59
|
|
|
* https://yii2framework.wordpress.com/2014/11/15/yii-2-behaviors-blameable-and-timestamp/comment-page-1/ |
60
|
|
|
* https://toster.ru/q/82962 |
61
|
|
|
* */ |
62
|
|
|
// If table not have fields, then behavior not use |
63
|
|
|
$behaviors = []; |
64
|
|
|
//Check timestamp |
65
|
|
|
if ($this->hasAttribute($this->createdAtAttribute) && $this->hasAttribute($this->updatedAtAttribute)) |
66
|
|
|
$behaviors['timestamp'] = [ |
67
|
|
|
'class' => TimestampBehavior::className(), |
68
|
|
|
'attributes' => [ |
69
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute], |
70
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute, |
71
|
|
|
], |
72
|
|
|
|
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
//Check blameable |
76
|
|
|
if ($this->hasAttribute($this->createdByAttribute) && $this->hasAttribute($this->updatedByAttribute)) |
77
|
|
|
$behaviors['blameable'] = [ |
78
|
|
|
'class' => UserDataBehavior::className(), |
79
|
|
|
'attributes' => [ |
80
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => [$this->createdByAttribute, $this->updatedByAttribute], |
81
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedByAttribute, |
82
|
|
|
], |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
//Check trash |
86
|
|
|
if ($this->hasAttribute($this->removedAttribute)) { |
87
|
|
|
$behaviors['trash'] = [ |
88
|
|
|
'class' => TrashBehavior::className(), |
89
|
|
|
'trashAttribute' => $this->removedAttribute, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//Check locked |
94
|
|
|
if ($this->hasAttribute($this->lockedAttribute)) { |
95
|
|
|
$behaviors['locked'] = [ |
96
|
|
|
'class' => LockedBehavior::className(), |
97
|
|
|
'lockedAttribute' => $this->lockedAttribute, |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $behaviors; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Duplicate entries in the table. |
106
|
|
|
* @return $this|null |
107
|
|
|
*/ |
108
|
|
|
public function duplicate(){ |
109
|
|
|
$this->isNewRecord=true; |
110
|
|
|
|
111
|
|
|
foreach($this->primaryKey() as $key) |
112
|
|
|
$this->$key = null; |
113
|
|
|
|
114
|
|
|
if($this->save()){ |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.