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锛
榛樿鎯呭喌涓嬶紝update灞炴у彧鍦ㄨ褰曡鏇存柊鏄墠琚缃傚鏋滀綘鎯宠鍏跺湪璁板綍琚垱寤虹殑鏃跺欎篃琚缃紝 鍙皢setUpdateOnCreate閫夐」璁剧疆涓簍rue銆
灏界CTimestampBehavior浼氳嚜琛屽喅瀹氬皢浠涔堟牱鐨勫煎啓鍏imestamp绫诲瀷鐨勫睘鎬э紝 浣犱粛鐒跺彲浠ラ氳繃timestampExpression鐢ㄨ嚜瀹氫箟鐨勫兼潵鏇夸唬瀹
浼氳嚜鍔ㄥ~鍏呮棩鏈熸椂闂村睘鎬у綋active record 琚垱寤轰互鍙/鎴栬呮洿鏂版椂銆 浣犲彲浠ュ儚涓嬮潰杩欐牱鎸囧畾涓涓猘ctive record妯″瀷绫绘潵浣跨敤姝ehavior锛
public function behaviors(){ return array( 'CTimestampBehavior' => array( 'class' => 'zii.behaviors.CTimestampBehavior', 'createAttribute' => 'create_time_attribute', 'updateAttribute' => 'update_time_attribute', ) ); }createAttribute鍜updateAttribute閫夐」瀹為檯涓婂垎鍒粯璁や负鈥榗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 |
鍏叡鏂规硶
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
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