AutoTimestamp.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\base\Behavior;
  9. use yii\base\Event;
  10. use yii\db\Expression;
  11. use yii\db\ActiveRecord;
  12. /**
  13. * AutoTimestamp will automatically fill the attributes about creation time and updating time.
  14. *
  15. * AutoTimestamp fills the attributes when the associated AR model is being inserted or updated.
  16. * You may specify an AR to use this behavior like the following:
  17. *
  18. * ~~~
  19. * public function behaviors()
  20. * {
  21. * return [
  22. * 'timestamp' => ['class' => 'yii\behaviors\AutoTimestamp'],
  23. * ];
  24. * }
  25. * ~~~
  26. *
  27. * By default, AutoTimestamp will fill the `created_at` attribute with the current timestamp
  28. * when the associated AR object is being inserted; it will fill the `updated_at` attribute
  29. * with the timestamp when the AR object is being updated.
  30. *
  31. * @author Qiang Xue <[email protected]>
  32. * @since 2.0
  33. */
  34. class AutoTimestamp extends Behavior
  35. {
  36. /**
  37. * @var array list of attributes that are to be automatically filled with timestamps.
  38. * The array keys are the ActiveRecord events upon which the attributes are to be filled with timestamps,
  39. * and the array values are the corresponding attribute(s) to be updated. You can use a string to represent
  40. * a single attribute, or an array to represent a list of attributes.
  41. * The default setting is to update the `created_at` attribute upon AR insertion,
  42. * and update the `updated_at` attribute upon AR updating.
  43. */
  44. public $attributes = [
  45. ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
  46. ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
  47. ];
  48. /**
  49. * @var \Closure|Expression The expression that will be used for generating the timestamp.
  50. * This can be either an anonymous function that returns the timestamp value,
  51. * or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`).
  52. * If not set, it will use the value of `time()` to fill the attributes.
  53. */
  54. public $timestamp;
  55. /**
  56. * Declares event handlers for the [[owner]]'s events.
  57. * @return array events (array keys) and the corresponding event handler methods (array values).
  58. */
  59. public function events()
  60. {
  61. $events = $this->attributes;
  62. foreach ($events as $i => $event) {
  63. $events[$i] = 'updateTimestamp';
  64. }
  65. return $events;
  66. }
  67. /**
  68. * Updates the attributes with the current timestamp.
  69. * @param Event $event
  70. */
  71. public function updateTimestamp($event)
  72. {
  73. $attributes = isset($this->attributes[$event->name]) ? (array)$this->attributes[$event->name] : [];
  74. if (!empty($attributes)) {
  75. $timestamp = $this->evaluateTimestamp();
  76. foreach ($attributes as $attribute) {
  77. $this->owner->$attribute = $timestamp;
  78. }
  79. }
  80. }
  81. /**
  82. * Gets the current timestamp.
  83. * @return mixed the timestamp value
  84. */
  85. protected function evaluateTimestamp()
  86. {
  87. if ($this->timestamp instanceof Expression) {
  88. return $this->timestamp;
  89. } elseif ($this->timestamp !== null) {
  90. return call_user_func($this->timestamp);
  91. } else {
  92. return time();
  93. }
  94. }
  95. /**
  96. * Updates a timestamp attribute to the current timestamp.
  97. *
  98. * ```php
  99. * $model->touch('lastVisit');
  100. * ```
  101. * @param string $attribute the name of the attribute to update.
  102. */
  103. public function touch($attribute)
  104. {
  105. $timestamp = $this->evaluateTimestamp();
  106. $this->owner->updateAttributes([$attribute => $timestamp]);
  107. }
  108. }