You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimestampBehavior.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. namespace yii\behaviors;
  8. use yii\db\BaseActiveRecord;
  9. use yii\db\Expression;
  10. /**
  11. * TimestampBehavior automatically fills the specified attributes with the current timestamp.
  12. *
  13. * To use TimestampBehavior, insert the following code to your ActiveRecord class:
  14. *
  15. * ```php
  16. * use yii\behaviors\TimestampBehavior;
  17. *
  18. * public function behaviors()
  19. * {
  20. * return [
  21. * TimestampBehavior::className(),
  22. * ];
  23. * }
  24. * ```
  25. *
  26. * By default, TimestampBehavior will fill the `created_at` and `updated_at` attributes with the current timestamp
  27. * when the associated AR object is being inserted; it will fill the `updated_at` attribute
  28. * with the timestamp when the AR object is being updated. The timestamp value is obtained by `time()`.
  29. *
  30. * If your attribute names are different or you want to use a different way of calculating the timestamp,
  31. * you may configure the [[createdAtAttribute]], [[updatedAtAttribute]] and [[value]] properties like the following:
  32. *
  33. * ```php
  34. * use yii\db\Expression;
  35. *
  36. * public function behaviors()
  37. * {
  38. * return [
  39. * [
  40. * 'class' => TimestampBehavior::className(),
  41. * 'createdAtAttribute' => 'create_time',
  42. * 'updatedAtAttribute' => 'update_time',
  43. * 'value' => new Expression('NOW()'),
  44. * ],
  45. * ];
  46. * }
  47. * ```
  48. *
  49. * In case you use an [[Expression]] object as in the example above, the attribute will not hold the timestamp value, but
  50. * the Expression object itself after the record has been saved. If you need the value from DB afterwards you should call
  51. * the [[\yii\db\ActiveRecord::refresh()|refresh()]] method of the record.
  52. *
  53. * TimestampBehavior also provides a method named [[touch()]] that allows you to assign the current
  54. * timestamp to the specified attribute(s) and save them to the database. For example,
  55. *
  56. * ```php
  57. * $model->touch('creation_time');
  58. * ```
  59. *
  60. * @author Qiang Xue <qiang.xue@gmail.com>
  61. * @author Alexander Kochetov <creocoder@gmail.com>
  62. * @since 2.0
  63. */
  64. class TimestampBehavior extends AttributeBehavior
  65. {
  66. /**
  67. * @var string the attribute that will receive timestamp value
  68. * Set this property to false if you do not want to record the creation time.
  69. */
  70. public $createdAtAttribute = 'created_at';
  71. /**
  72. * @var string the attribute that will receive timestamp value.
  73. * Set this property to false if you do not want to record the update time.
  74. */
  75. public $updatedAtAttribute = 'updated_at';
  76. /**
  77. * @var callable|Expression The expression that will be used for generating the timestamp.
  78. * This can be either an anonymous function that returns the timestamp value,
  79. * or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`).
  80. * If not set, it will use the value of `time()` to set the attributes.
  81. */
  82. public $value;
  83. /**
  84. * @inheritdoc
  85. */
  86. public function init()
  87. {
  88. parent::init();
  89. if (empty($this->attributes)) {
  90. $this->attributes = [
  91. BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute],
  92. BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute,
  93. ];
  94. }
  95. }
  96. /**
  97. * @inheritdoc
  98. */
  99. protected function getValue($event)
  100. {
  101. if ($this->value instanceof Expression) {
  102. return $this->value;
  103. } else {
  104. return $this->value !== null ? call_user_func($this->value, $event) : time();
  105. }
  106. }
  107. /**
  108. * Updates a timestamp attribute to the current timestamp.
  109. *
  110. * ```php
  111. * $model->touch('lastVisit');
  112. * ```
  113. * @param string $attribute the name of the attribute to update.
  114. */
  115. public function touch($attribute)
  116. {
  117. $this->owner->updateAttributes(array_fill_keys((array) $attribute, $this->getValue(null)));
  118. }
  119. }