Yii Framework v1.1.10 绫诲弬鑰

CTimestampBehavior

zii.behaviors
缁ф壙 class CTimestampBehavior » CActiveRecordBehavior » CModelBehavior » CBehavior » CComponent
瀹炵幇 IBehavior
婧愯嚜 1.1
鐗堟湰 $Id: CTimestampBehavior.php 3229 2011-05-21 00:20:29Z alexander.makarow $
婧愮爜 framework/zii/behaviors/CTimestampBehavior.php
CTimestampBehavior浼氳嚜鍔ㄥ~鍏呮棩鏈熷拰鏃堕棿鐩稿叧鐨勫睘鎬с

浼氳嚜鍔ㄥ~鍏呮棩鏈熸椂闂村睘鎬у綋active record 琚垱寤轰互鍙/鎴栬呮洿鏂版椂銆 浣犲彲浠ュ儚涓嬮潰杩欐牱鎸囧畾涓涓猘ctive record妯″瀷绫绘潵浣跨敤姝ehavior锛
public function behaviors(){
	return array(
		'CTimestampBehavior' => array(
			'class' => 'zii.behaviors.CTimestampBehavior',
			'createAttribute' => 'create_time_attribute',
			'updateAttribute' => 'update_time_attribute',
		)
	);
}
createAttributeupdateAttribute閫夐」瀹為檯涓婂垎鍒粯璁や负鈥榗reate_time鈥欏拰鈥榰pdate_time鈥欙紝 鍥犳璁剧疆瀹冧滑骞堕潪蹇呴』銆傚鏋滀綘涓嶅笇鏈汣TimestampBehavior 涓鸿record鐨勬洿鏂版垨鍒涘缓璁剧疆涓涓猼imestamp鍊硷紝鍙互璁惧畾鐩稿簲鐨勫睘鎬ч夐」涓簄ull銆

榛樿鎯呭喌涓嬶紝update灞炴у彧鍦ㄨ褰曡鏇存柊鏄墠琚缃傚鏋滀綘鎯宠鍏跺湪璁板綍琚垱寤虹殑鏃跺欎篃琚缃紝 鍙皢setUpdateOnCreate閫夐」璁剧疆涓簍rue銆

灏界CTimestampBehavior浼氳嚜琛屽喅瀹氬皢浠涔堟牱鐨勫煎啓鍏imestamp绫诲瀷鐨勫睘鎬э紝 浣犱粛鐒跺彲浠ラ氳繃timestampExpression鐢ㄨ嚜瀹氫箟鐨勫兼潵鏇夸唬瀹

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
createAttribute mixed 鐢ㄦ潵瀛樺偍鍒涘缓鏃堕棿灞炴х殑鍚嶇О銆 璁剧疆涓簄ull鏄垱寤哄睘鎬т笉浣跨敤涓涓椂闂存埑銆傞粯璁ゆ槸鈥榗reate_time鈥 CTimestampBehavior
enabled boolean 浜嬩欢鏄惁琚惎鐢ㄣ CBehavior
owner CComponent 鑾峰緱闄勫姞琛屼负鐨勭粍浠躲 CBehavior
setUpdateOnCreate bool 鏇存柊灞炴ф槸鍚﹁缃湪鍒涘缓鏃跺垱寤烘椂闂存埑銆 鍚﹀垯灏嗚鍗曠嫭鐣欎笅銆傞粯璁や负false銆 CTimestampBehavior
timestampExpression mixed 灏嗙敤浜庣敓鎴愭椂闂存埑鐨勮〃杈惧紡銆 瀹冨彲浠ユ槸涓涓瓧绗︿覆锛屼唬琛ㄤ竴涓狿HP琛ㄨ揪寮忥紙渚嬪锛'itme()'锛夛紝 鎴栬呬竴涓CDbExpression瀵硅薄浠h〃涓涓狣B琛ㄨ揪寮忥紙渚嬪锛歯ew CDbExpression('NOW()')锛夈 榛樿涓虹┖锛岃繖鎰忓懗鐫鎴戜滑灏嗗皾璇曡嚜鍔ㄨ绠楀嚭鐩稿簲鐨勬椂闂存埑銆 濡傛灉鎴戜滑涓嶈兘鎵惧埌鍚堥傜殑鏃堕棿鎴筹紝 鐒跺悗锛屽畠浼氬悗閫鍒板綋鍓嶇殑UNIX鏃堕棿鎴 CTimestampBehavior
updateAttribute mixed 鐢ㄦ潵瀛樺偍淇敼鏃堕棿灞炴х殑鍚嶇О銆 璁剧疆涓簄ull鏄洿鏂板睘鎬т笉浣跨敤涓涓椂闂存埑銆傞粯璁ゆ槸鈥榰pdate_time鈥 CTimestampBehavior

鍙椾繚鎶ゅ睘鎬

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
map array 鏄犲皠鍒楃被鍨嬪埌鏁版嵁搴撶殑鏂规硶 CTimestampBehavior

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
afterConstruct() 鍝嶅簲CModel::onAfterConstruct浜嬩欢銆 CModelBehavior
afterDelete() 鍝嶅簲鍒 CActiveRecord::onAfterDelete 浜嬩欢銆 CActiveRecordBehavior
afterFind() 鍝嶅簲鍒 CActiveRecord::onAfterFind 浜嬩欢銆 CActiveRecordBehavior
afterSave() 鍝嶅簲鍒 CActiveRecord::onAfterSave 浜嬩欢銆 CActiveRecordBehavior
afterValidate() 鍝嶅簲CModel::onAfterValidate浜嬩欢銆 CModelBehavior
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attach() 闄勫姞浜嬩欢澶勭悊绋嬪簭鍒扮粍浠躲 CBehavior
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
beforeDelete() 鍝嶅簲鍒 CActiveRecord::onBeforeDelete 浜嬩欢銆 CActiveRecordBehavior
beforeFind() 鍝嶅簲鍒 CActiveRecord::onBeforeFind 浜嬩欢銆 CActiveRecordBehavior
beforeSave() 鍝嶅簲CModel::onBeforeSave浜嬩欢銆 CTimestampBehavior
beforeValidate() 鍝嶅簲CModel::onBeforeValidate浜嬩欢銆 CModelBehavior
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
detach() 浠庣粍浠跺垎绂昏涓哄璞° CBehavior
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
events() 瀹氫箟浜嬩欢鍜岀浉搴旂殑浜嬩欢澶勭悊鏂规硶銆 CActiveRecordBehavior
getEnabled() 杩斿洖浜嬩欢鏄惁琚惎鐢ㄣ CBehavior
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getOwner() 杩斿洖鑾峰緱闄勫姞琛屼负鐨勭粍浠躲 CBehavior
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
setEnabled() 璁剧疆鏄惁鍚敤琛屼负 CBehavior

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
getTimestampByAttribute() 鑾峰緱鍚堥傜殑鏃堕棿鎴冲彇鍐充簬鍒楃被鍨$attribute CTimestampBehavior
getTimestampByColumnType() 杩斿洖鍚堥傜殑鏃堕棿鎴冲彇鍐充簬$columnType CTimestampBehavior

灞炴ц缁

createAttribute 灞炴
public mixed $createAttribute;

鐢ㄦ潵瀛樺偍鍒涘缓鏃堕棿灞炴х殑鍚嶇О銆 璁剧疆涓簄ull鏄垱寤哄睘鎬т笉浣跨敤涓涓椂闂存埑銆傞粯璁ゆ槸鈥榗reate_time鈥

map 灞炴
protected static array $map;

鏄犲皠鍒楃被鍨嬪埌鏁版嵁搴撶殑鏂规硶

setUpdateOnCreate 灞炴
public bool $setUpdateOnCreate;

鏇存柊灞炴ф槸鍚﹁缃湪鍒涘缓鏃跺垱寤烘椂闂存埑銆 鍚﹀垯灏嗚鍗曠嫭鐣欎笅銆傞粯璁や负false銆

timestampExpression 灞炴
public mixed $timestampExpression;

灏嗙敤浜庣敓鎴愭椂闂存埑鐨勮〃杈惧紡銆 瀹冨彲浠ユ槸涓涓瓧绗︿覆锛屼唬琛ㄤ竴涓狿HP琛ㄨ揪寮忥紙渚嬪锛'itme()'锛夛紝 鎴栬呬竴涓CDbExpression瀵硅薄浠h〃涓涓狣B琛ㄨ揪寮忥紙渚嬪锛歯ew CDbExpression('NOW()')锛夈 榛樿涓虹┖锛岃繖鎰忓懗鐫鎴戜滑灏嗗皾璇曡嚜鍔ㄨ绠楀嚭鐩稿簲鐨勬椂闂存埑銆 濡傛灉鎴戜滑涓嶈兘鎵惧埌鍚堥傜殑鏃堕棿鎴筹紝 鐒跺悗锛屽畠浼氬悗閫鍒板綋鍓嶇殑UNIX鏃堕棿鎴

updateAttribute 灞炴
public mixed $updateAttribute;

鐢ㄦ潵瀛樺偍淇敼鏃堕棿灞炴х殑鍚嶇О銆 璁剧疆涓簄ull鏄洿鏂板睘鎬т笉浣跨敤涓涓椂闂存埑銆傞粯璁ゆ槸鈥榰pdate_time鈥

鏂规硶璇︾粏

beforeSave() 鏂规硶
public void beforeSave(CModelEvent $event)
$event CModelEvent 浜嬩欢鍙傛暟
婧愮爜锛 framework/zii/behaviors/CTimestampBehavior.php#87 (鏄剧ず)
public function beforeSave($event) {
    if (
$this->getOwner()->getIsNewRecord() && ($this->createAttribute !== null)) {
        
$this->getOwner()->{$this->createAttribute} = $this->getTimestampByAttribute($this->createAttribute);
    }
    if ((!
$this->getOwner()->getIsNewRecord() || $this->setUpdateOnCreate) && ($this->updateAttribute !== null)) {
        
$this->getOwner()->{$this->updateAttribute} = $this->getTimestampByAttribute($this->updateAttribute);
    }
}

鍝嶅簲CModel::onBeforeSave浜嬩欢銆 璁剧疆鍒涘缓鎴栦慨鏀圭殑灞炴у肩殑閰嶇疆

getTimestampByAttribute() 鏂规硶
protected mixed getTimestampByAttribute(string $attribute)
$attribute string $attribute
{return} mixed 鏃堕棿鎴筹紙渚嬪锛寀nix鏃堕棿鎴虫垨鑰呬竴涓猰ysql鍑芥暟锛
婧愮爜锛 framework/zii/behaviors/CTimestampBehavior.php#102 (鏄剧ず)
protected function getTimestampByAttribute($attribute) {
    if (
$this->timestampExpression instanceof CDbExpression)
        return 
$this->timestampExpression;
    else if (
$this->timestampExpression !== null)
        return @eval(
'return '.$this->timestampExpression.';');

    
$columnType $this->getOwner()->getTableSchema()->getColumn($attribute)->dbType;
    return 
$this->getTimestampByColumnType($columnType);
}

鑾峰緱鍚堥傜殑鏃堕棿鎴冲彇鍐充簬鍒楃被鍨$attribute

getTimestampByColumnType() 鏂规硶
protected mixed getTimestampByColumnType(string $columnType)
$columnType string $columnType
{return} mixed 鏃堕棿鎴筹紙渚嬪锛寀nix鏃堕棿鎴虫垨鑰呬竴涓猰ysql鍑芥暟锛
婧愮爜锛 framework/zii/behaviors/CTimestampBehavior.php#118 (鏄剧ず)
protected function getTimestampByColumnType($columnType) {
    return isset(
self::$map[$columnType]) ? new CDbExpression(self::$map[$columnType]) : time();
}

杩斿洖鍚堥傜殑鏃堕棿鎴冲彇鍐充簬$columnType

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.