Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

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