1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\behaviors; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidCallException; |
11
|
|
|
use yii\db\BaseActiveRecord; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* TimestampBehavior automatically fills the specified attributes with the current timestamp. |
15
|
|
|
* |
16
|
|
|
* To use TimestampBehavior, insert the following code to your ActiveRecord class: |
17
|
|
|
* |
18
|
|
|
* ```php |
19
|
|
|
* use yii\behaviors\TimestampBehavior; |
20
|
|
|
* |
21
|
|
|
* public function behaviors() |
22
|
|
|
* { |
23
|
|
|
* return [ |
24
|
|
|
* TimestampBehavior::className(), |
25
|
|
|
* ]; |
26
|
|
|
* } |
27
|
|
|
* ``` |
28
|
|
|
* |
29
|
|
|
* By default, TimestampBehavior will fill the `created_at` and `updated_at` attributes with the current timestamp |
30
|
|
|
* when the associated AR object is being inserted; it will fill the `updated_at` attribute |
31
|
|
|
* with the timestamp when the AR object is being updated. The timestamp value is obtained by `time()`. |
32
|
|
|
* |
33
|
|
|
* Because attribute values will be set automatically by this behavior, they are usually not user input and should therefore |
34
|
|
|
* not be validated, i.e. `created_at` and `updated_at` should not appear in the [[\yii\base\Model::rules()|rules()]] method of the model. |
35
|
|
|
* |
36
|
|
|
* For the above implementation to work with MySQL database, please declare the columns(`created_at`, `updated_at`) as int(11) for being UNIX timestamp. |
37
|
|
|
* |
38
|
|
|
* If your attribute names are different or you want to use a different way of calculating the timestamp, |
39
|
|
|
* you may configure the [[createdAtAttribute]], [[updatedAtAttribute]] and [[value]] properties like the following: |
40
|
|
|
* |
41
|
|
|
* ```php |
42
|
|
|
* use yii\db\Expression; |
43
|
|
|
* |
44
|
|
|
* public function behaviors() |
45
|
|
|
* { |
46
|
|
|
* return [ |
47
|
|
|
* [ |
48
|
|
|
* 'class' => TimestampBehavior::className(), |
49
|
|
|
* 'createdAtAttribute' => 'create_time', |
50
|
|
|
* 'updatedAtAttribute' => 'update_time', |
51
|
|
|
* 'value' => new Expression('NOW()'), |
52
|
|
|
* ], |
53
|
|
|
* ]; |
54
|
|
|
* } |
55
|
|
|
* ``` |
56
|
|
|
* |
57
|
|
|
* In case you use an [[\yii\db\Expression]] object as in the example above, the attribute will not hold the timestamp value, but |
58
|
|
|
* the Expression object itself after the record has been saved. If you need the value from DB afterwards you should call |
59
|
|
|
* the [[\yii\db\ActiveRecord::refresh()|refresh()]] method of the record. |
60
|
|
|
* |
61
|
|
|
* TimestampBehavior also provides a method named [[touch()]] that allows you to assign the current |
62
|
|
|
* timestamp to the specified attribute(s) and save them to the database. For example, |
63
|
|
|
* |
64
|
|
|
* ```php |
65
|
|
|
* $model->touch('creation_time'); |
66
|
|
|
* ``` |
67
|
|
|
* |
68
|
|
|
* @author Qiang Xue <[email protected]> |
69
|
|
|
* @author Alexander Kochetov <[email protected]> |
70
|
|
|
* @since 2.0 |
71
|
|
|
*/ |
72
|
|
|
class TimestampBehavior extends AttributeBehavior |
73
|
|
|
{ |
74
|
|
|
/** |
75
|
|
|
* @var string the attribute that will receive timestamp value |
76
|
|
|
* Set this property to false if you do not want to record the creation time. |
77
|
|
|
*/ |
78
|
|
|
public $createdAtAttribute = 'created_at'; |
79
|
|
|
/** |
80
|
|
|
* @var string the attribute that will receive timestamp value. |
81
|
|
|
* Set this property to false if you do not want to record the update time. |
82
|
|
|
*/ |
83
|
|
|
public $updatedAtAttribute = 'updated_at'; |
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
* |
87
|
|
|
* In case, when the value is `null`, the result of the PHP function [time()](http://php.net/manual/en/function.time.php) |
88
|
|
|
* will be used as value. |
89
|
|
|
*/ |
90
|
|
|
public $value; |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
12 |
|
public function init() |
97
|
|
|
{ |
98
|
12 |
|
parent::init(); |
99
|
|
|
|
100
|
12 |
|
if (empty($this->attributes)) { |
101
|
12 |
|
$this->attributes = [ |
102
|
12 |
|
BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute], |
103
|
12 |
|
BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute, |
104
|
|
|
]; |
105
|
|
|
} |
106
|
12 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
* |
111
|
|
|
* In case, when the [[value]] is `null`, the result of the PHP function [time()](http://php.net/manual/en/function.time.php) |
112
|
|
|
* will be used as value. |
113
|
|
|
*/ |
114
|
11 |
|
protected function getValue($event) |
115
|
|
|
{ |
116
|
11 |
|
if ($this->value === null) { |
117
|
4 |
|
return time(); |
118
|
|
|
} |
119
|
7 |
|
return parent::getValue($event); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Updates a timestamp attribute to the current timestamp. |
124
|
|
|
* |
125
|
|
|
* ```php |
126
|
|
|
* $model->touch('lastVisit'); |
127
|
|
|
* ``` |
128
|
|
|
* @param string $attribute the name of the attribute to update. |
129
|
|
|
* @throws InvalidCallException if owner is a new record (since version 2.0.6). |
130
|
|
|
*/ |
131
|
2 |
|
public function touch($attribute) |
132
|
|
|
{ |
133
|
|
|
/* @var $owner BaseActiveRecord */ |
134
|
2 |
|
$owner = $this->owner; |
135
|
2 |
|
if ($owner->getIsNewRecord()) { |
136
|
1 |
|
throw new InvalidCallException('Updating the timestamp is not possible on a new record.'); |
137
|
|
|
} |
138
|
1 |
|
$owner->updateAttributes(array_fill_keys((array) $attribute, $this->getValue(null))); |
139
|
1 |
|
} |
140
|
|
|
} |
141
|
|
|
|