AutoTimestamp.php 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\behaviors;

use yii\base\Behavior;
Qiang Xue committed
11
use yii\base\Event;
12 13 14 15 16 17 18 19 20 21 22 23
use yii\db\Expression;
use yii\db\ActiveRecord;

/**
 * AutoTimestamp will automatically fill the attributes about creation time and updating time.
 *
 * AutoTimestamp fills the attributes when the associated AR model is being inserted or updated.
 * You may specify an AR to use this behavior like the following:
 *
 * ~~~
 * public function behaviors()
 * {
Alexander Makarov committed
24 25 26
 *     return [
 *         'timestamp' => ['class' => 'yii\behaviors\AutoTimestamp'],
 *     ];
27 28 29
 * }
 * ~~~
 *
30
 * By default, AutoTimestamp will fill the `create_time` attribute with the current timestamp
Qiang Xue committed
31 32
 * when the associated AR object is being inserted; it will fill the `update_time` attribute
 * with the timestamp when the AR object is being updated.
33 34 35 36 37 38 39
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AutoTimestamp extends Behavior
{
	/**
Qiang Xue committed
40 41 42 43
	 * @var array list of attributes that are to be automatically filled with timestamps.
	 * The array keys are the ActiveRecord events upon which the attributes are to be filled with timestamps,
	 * and the array values are the corresponding attribute to be updated. You can use a string to represent
	 * a single attribute, or an array to represent a list of attributes.
44
	 * The default setting is to update the `create_time` attribute upon AR insertion,
Qiang Xue committed
45
	 * and update the `update_time` attribute upon AR updating.
46
	 */
Alexander Makarov committed
47
	public $attributes = [
48
		ActiveRecord::EVENT_BEFORE_INSERT => 'create_time',
Qiang Xue committed
49
		ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
Alexander Makarov committed
50
	];
51
	/**
52 53
	 * @var \Closure|Expression The expression that will be used for generating the timestamp.
	 * This can be either an anonymous function that returns the timestamp value,
54 55 56 57 58 59 60 61 62 63 64 65
	 * or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`).
	 * If not set, it will use the value of `time()` to fill the attributes.
	 */
	public $timestamp;


	/**
	 * Declares event handlers for the [[owner]]'s events.
	 * @return array events (array keys) and the corresponding event handler methods (array values).
	 */
	public function events()
	{
Qiang Xue committed
66 67 68
		$events = $this->attributes;
		foreach ($events as $i => $event) {
			$events[$i] = 'updateTimestamp';
69
		}
Qiang Xue committed
70
		return $events;
71 72 73
	}

	/**
Qiang Xue committed
74
	 * Updates the attributes with the current timestamp.
Qiang Xue committed
75
	 * @param Event $event
76
	 */
Qiang Xue committed
77
	public function updateTimestamp($event)
78
	{
Alexander Makarov committed
79
		$attributes = isset($this->attributes[$event->name]) ? (array)$this->attributes[$event->name] : [];
Qiang Xue committed
80 81 82 83 84
		if (!empty($attributes)) {
			$timestamp = $this->evaluateTimestamp();
			foreach ($attributes as $attribute) {
				$this->owner->$attribute = $timestamp;
			}
85 86 87 88
		}
	}

	/**
89
	 * Gets the current timestamp.
90 91
	 * @return mixed the timestamp value
	 */
92
	protected function evaluateTimestamp()
93 94 95 96
	{
		if ($this->timestamp instanceof Expression) {
			return $this->timestamp;
		} elseif ($this->timestamp !== null) {
97
			return call_user_func($this->timestamp);
98 99 100 101 102
		} else {
			return time();
		}
	}
}