1 | <?php |
||
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) |
|
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) |
|
140 | } |
||
141 |