resurtm

Yii2 Timestamp Behavior

May 10th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. <?php
  2.  
  3. namespace common\components;
  4.  
  5. use \yii\base\Behavior;
  6. use \yii\base\ModelEvent;
  7. use \yii\db\ActiveRecord;
  8. use \yii\db\Expression;
  9.  
  10. /**
  11.  *
  12.  */
  13. class TimestampBehavior extends Behavior
  14. {
  15.     /**
  16.      * @var string
  17.      */
  18.     public $createAttribute = 'created_at';
  19.  
  20.     /**
  21.      * @var string
  22.      */
  23.     public $updateAttribute = 'updated_at';
  24.  
  25.     /**
  26.      * @var boolean
  27.      */
  28.     public $setUpdateOnCreate = true;
  29.  
  30.     /**
  31.      * @var string
  32.      */
  33.     public $timestampExpression;
  34.  
  35.     /**
  36.      * @var array
  37.      */
  38.     public static $typeMap = array(
  39.         'datetime' => 'NOW()',
  40.         'timestamp' => 'NOW()',
  41.         'date' => 'NOW()',
  42.     );
  43.  
  44.     public function events()
  45.     {
  46.         return array(
  47.             ActiveRecord::EVENT_BEFORE_INSERT => 'onInsert',
  48.             ActiveRecord::EVENT_BEFORE_UPDATE => 'onUpdate',
  49.         );
  50.     }
  51.  
  52.     /**
  53.      * @param ModelEvent $event
  54.      */
  55.     public function onInsert($event)
  56.     {
  57.         if ($this->createAttribute !== null) {
  58.             $this->owner->{$this->createAttribute} = $this->getTimestampByAttribute($this->createAttribute);
  59.         }
  60.         if ($this->updateAttribute !== null && $this->setUpdateOnCreate) {
  61.             $this->owner->{$this->updateAttribute} = $this->getTimestampByAttribute($this->updateAttribute);
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * @param ModelEvent $event
  67.      */
  68.     public function onUpdate($event)
  69.     {
  70.         if ($this->updateAttribute !== null) {
  71.             $this->owner->{$this->updateAttribute} = $this->getTimestampByAttribute($this->updateAttribute);
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * @param string $attribute
  77.      * @return integer|Expression
  78.      */
  79.     protected function getTimestampByAttribute($attribute)
  80.     {
  81.         if ($this->timestampExpression instanceof Expression) {
  82.             return $this->timestampExpression;
  83.         } elseif ($this->timestampExpression !== null) {
  84.             return eval('return ' . $this->timestampExpression . ';');
  85.         }
  86.  
  87.         $type = $this->owner->getTableSchema()->getColumn($attribute)->type;
  88.         return $this->getTimestampByType($type);
  89.     }
  90.  
  91.     /**
  92.      * @param string $type
  93.      * @return integer|Expression
  94.      */
  95.     protected function getTimestampByType($type)
  96.     {
  97.         if (isset(static::$typeMap[$type])) {
  98.             return new Expression(static::$typeMap[$type]);
  99.         } else {
  100.             return time();
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment