CTimestampBehavior.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * CTimestampBehavior class file.
  4. *
  5. * @author Jonah Turnquist <poppitypop@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CTimestampBehavior will automatically fill date and time related attributes.
  12. *
  13. * CTimestampBehavior will automatically fill date and time related attributes when the active record
  14. * is created and/or updated.
  15. * You may specify an active record model to use this behavior like so:
  16. * <pre>
  17. * public function behaviors(){
  18. * return array(
  19. * 'CTimestampBehavior' => array(
  20. * 'class' => 'zii.behaviors.CTimestampBehavior',
  21. * 'createAttribute' => 'create_time_attribute',
  22. * 'updateAttribute' => 'update_time_attribute',
  23. * )
  24. * );
  25. * }
  26. * </pre>
  27. * The {@link createAttribute} and {@link updateAttribute} options actually default to 'create_time' and 'update_time'
  28. * respectively, so it is not required that you configure them. If you do not wish CTimestampBehavior
  29. * to set a timestamp for record update or creation, set the corresponding attribute option to null.
  30. *
  31. * By default, the update attribute is only set on record update. If you also wish it to be set on record creation,
  32. * set the {@link setUpdateOnCreate} option to true.
  33. *
  34. * Although CTimestampBehavior attempts to figure out on it's own what value to inject into the timestamp attribute,
  35. * you may specify a custom value to use instead via {@link timestampExpression}
  36. *
  37. * @author Jonah Turnquist <poppitypop@gmail.com>
  38. * @package zii.behaviors
  39. * @since 1.1
  40. */
  41. class CTimestampBehavior extends CActiveRecordBehavior {
  42. /**
  43. * @var mixed The name of the attribute to store the creation time. Set to null to not
  44. * use a timestamp for the creation attribute. Defaults to 'create_time'
  45. */
  46. public $createAttribute = 'create_time';
  47. /**
  48. * @var mixed The name of the attribute to store the modification time. Set to null to not
  49. * use a timestamp for the update attribute. Defaults to 'update_time'
  50. */
  51. public $updateAttribute = 'update_time';
  52. /**
  53. * @var bool Whether to set the update attribute to the creation timestamp upon creation.
  54. * Otherwise it will be left alone. Defaults to false.
  55. */
  56. public $setUpdateOnCreate = false;
  57. /**
  58. * @var mixed The expression that will be used for generating the timestamp.
  59. * This can be either a string representing a PHP expression (e.g. 'time()'),
  60. * or a {@link CDbExpression} object representing a DB expression (e.g. new CDbExpression('NOW()')).
  61. * Defaults to null, meaning that we will attempt to figure out the appropriate timestamp
  62. * automatically. If we fail at finding the appropriate timestamp, then it will
  63. * fall back to using the current UNIX timestamp.
  64. *
  65. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  66. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  67. */
  68. public $timestampExpression;
  69. /**
  70. * @var array Maps column types to database method
  71. */
  72. protected static $map = array(
  73. 'datetime'=>'NOW()',
  74. 'timestamp'=>'NOW()',
  75. 'date'=>'NOW()',
  76. );
  77. /**
  78. * Responds to {@link CModel::onBeforeSave} event.
  79. * Sets the values of the creation or modified attributes as configured
  80. *
  81. * @param CModelEvent $event event parameter
  82. */
  83. public function beforeSave($event) {
  84. if ($this->getOwner()->getIsNewRecord() && ($this->createAttribute !== null)) {
  85. $this->getOwner()->{$this->createAttribute} = $this->getTimestampByAttribute($this->createAttribute);
  86. }
  87. if ((!$this->getOwner()->getIsNewRecord() || $this->setUpdateOnCreate) && ($this->updateAttribute !== null)) {
  88. $this->getOwner()->{$this->updateAttribute} = $this->getTimestampByAttribute($this->updateAttribute);
  89. }
  90. }
  91. /**
  92. * Gets the appropriate timestamp depending on the column type $attribute is
  93. *
  94. * @param string $attribute $attribute
  95. * @return mixed timestamp (eg unix timestamp or a mysql function)
  96. */
  97. protected function getTimestampByAttribute($attribute) {
  98. if ($this->timestampExpression instanceof CDbExpression)
  99. return $this->timestampExpression;
  100. elseif ($this->timestampExpression !== null)
  101. return @eval('return '.$this->timestampExpression.';');
  102. $columnType = $this->getOwner()->getTableSchema()->getColumn($attribute)->dbType;
  103. return $this->getTimestampByColumnType($columnType);
  104. }
  105. /**
  106. * Returns the appropriate timestamp depending on $columnType
  107. *
  108. * @param string $columnType $columnType
  109. * @return mixed timestamp (eg unix timestamp or a mysql function)
  110. */
  111. protected function getTimestampByColumnType($columnType) {
  112. return isset(self::$map[$columnType]) ? new CDbExpression(self::$map[$columnType]) : time();
  113. }
  114. }